• Tkinter:文本框Entry


    文本框Entry

    文本框基本概念

    单行的文本框,是用于输入的最基本Widget控件,可以使用它输入单行字符串,如果所输入的字符串长度大于文本框的宽度,所输入的文字会自动隐藏造成部分内容无法显示。

    文本框Entry限定是单行文字,如果想要处理多行文字需使用Widget控件中的Text

    Entry(父对象,options,...)
    

    Entry( )方法的第一个参数是父对象,表示这个文本框将建立在哪一个窗口内。

    options参数
    1. bg或background:背景色彩
    2. borderwidth或bd:边界宽度默认是2像素
    3. command:当用户更改内容时,会自动执行此函数
    4. cursor:当鼠标光标在复选框上时的光标形状
    5. exportselection:如果执行选取时,所选取的字符串会自动输出至剪贴板,如果想要避免,可以设置exportselection=0
    6. fg或foreground:前景色彩
    7. font:字形
    8. height:高,单位是字符高
    9. highlightbackground:当文本框取得焦点时的背景颜色
    10. highlightcolor:当文本框取得焦点时的颜色
    11. justify:当含多行文字时,最后一行的对齐方式
    12. relief:默认是relief=FLAT,可由此控制文字外框
    13. selectbackground:被选取字符串的背景色彩
    14. selectborderwidth:选取字符串时的边界宽度,预设是1
    15. selectfroeground:被选取字符串的前景色彩
    16. show:显示输入字符,例如,show='*'表示显示星号,常用于输入密码字段
    17. state:输入状态,默认是NORMAL表示可以输入,DISABLE则表示无法输入
    18. textvariable:文字变量
    19. width:宽,单位是字符宽
    20. xscrollcommand:在x轴使用滚动条

    例子

    建立标签和文本框,输入姓名和地址

    from tkinter import *
    root = Tk()
    root.title("demo")
    nameL = Label(root,text="Name")
    addressL= Label(root,text="Address")
    nameE = Entry(root)
    addressE = Entry(root)
    
    #布局
    nameL.grid(row=0)
    addressL.grid(row=1)
    nameE.grid(row=0,column=1)
    addressE.grid(row=1,column=1)
    
    root.mainloop()
    

    grid(row=0),在没有设置“column=x”的情况下,系统将自动设置“column=0”

    使用show隐藏字符

    show参数设置隐藏输入字符,常被应用于密码的输入控制

    当输入密码时所输入的字符将隐藏并用“*”字符

    from tkinter import *
    root = Tk()
    root.title("demo")
    nameL = Label(root,text="Name")
    pwdL= Label(root,text="Pwd")
    nameE = Entry(root)
    pwdE = Entry(root,show="*")
    
    #布局
    nameL.grid(row=0)
    pwdL.grid(row=1)
    nameE.grid(row=0,column=1)
    pwdE.grid(row=1,column=1)
    
    root.mainloop()
    

    在这里插入图片描述

    get()方法

    获得目前Entry的字符串内容

    Widget控件有一个常用方法Quit,执行此方法时Python Shell窗口的程序将结束,但是此窗口应用程序继续运行。

    例子

    1. Login和Quit功能按钮
    2. 单击Login功能按钮,列出所输入的Account和Password
    3. 单击Quit按钮,则Python Shell窗口中程序执行结束,但是屏幕上仍可以看到此应用窗口
    from tkinter import *
    
    def printInfo():
        print("Account: %s\r\nPwd: %s"%(accountE.get(),pwdE.get()))
        
    root = Tk()
    root.title("demo")
    
    accountL = Label(root,text="Account")
    pwdL= Label(root,text="Pwd")
    
    accountE = Entry(root)
    pwdE = Entry(root,show="*")
    
    loginbtn = Button(root,text="Login",command=printInfo)
    quitbtn = Button(root,text="Quit",command=root.quit)
    
    #布局
    accountL.grid(row=0)
    pwdL.grid(row=1)
    accountE.grid(row=0,column=1)
    pwdE.grid(row=1,column=1)
    loginbtn.grid(row=2,column=0,sticky=W,pady=5,padx=5)
    quitbtn.grid(row=2,column=1,sticky=E,pady=5,padx=5)
    
    root.mainloop()
    
    insert()方法

    insert(index,s)方法在Widget的Entry控件中插入字符串,字符串会插入在index位置

    可以使用这个方法为文本框建立默认的文字,通常会将它放在Entry( )方法建立完文本框后

    accountE = Entry(root)
    pwdE = Entry(root,show="*")
    
    accountE.insert(0,"wkk") #默认内容
    pwdE.insert(0,"wkk")     #默认内容
    

    在这里插入图片描述

    delete()方法

    delete(first,last=None) 方法删除Entry内的从第first字符到第last-1字符间的字符串

    删除整个字符串可以使用delete(0,END)

    accountE.delete(0,END) # 删除所有内容
    pwdE.delete(0,END)
    
    例子:计算
    eval()函数介绍

    该函数可以直接传回字符串数学表达式的计算结果

    result = eval(expression) # expression是字符串
    
    demo
    from tkinter import *
    def cal():
        out["text"] =  "结果: " + str(eval(equ.get()))
        
    root = Tk()
    root.title("calc")
    
    label = Label(root,text="请输入数学表达式")
    equ = Entry(root)
    btn = Button(root,text="计算", command=cal)
    out = Label(root)
    
    label.pack()
    equ.pack(pady=5)
    out.pack()
    btn.pack(pady=5)
    
    root.mainloop()
    
  • 相关阅读:
    数据结构与算法--并查集结构
    LeetCode 1123. 最深叶节点的最近公共祖先:DFS
    NOSQL之Redis配置与优化
    BEV经典之作Lift, Splat, Shoot解析
    【学习笔记之vue】vue项目放在springboot项目里后,刷新页面会显示whitelabel error page
    下沉一线农技志愿服务 国稻种芯-芜湖:湾沚红杨护秋粮生产
    Javaweb03-servlet&filter
    架构问题自查
    多测师肖sir_高级金牌讲师__git讲解
    Flask 学习-13.Flask-SQLAlchemy 新建模型和字段
  • 原文地址:https://blog.csdn.net/first_bug/article/details/127088843