五、文本框Entry

文本框Entry的基本概念

所谓的文本框Entry,通常是指单行的文本框,在GUI程序设计中这是用手输入的最基本
Widget控件,我们可以使用它输入单行字符串,如果所输入的字符串长度大于文本框的
宽度,所输入的文字会自动隐藏造成部分内容无法显示。碰到这种状况时,可以使用
箭头键移动鼠标光标到看不到的区域。需留意的是文本框Entry限定是单行文字,如果
想要处理多行文字需使用Widget控件中的Text。它的使用格式如下:
Entry(父对象,option,···)

Entry()方法的第一个参数是父对象,表示这个文本框将建立在哪一个窗口内。下面是
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("ch5_1")
nameL=Label(root,text="Name ")
nameL.grid(row=0)
addressL=Label(root,text="Address")
addressL.grid(row=1)
nameE=Entry(root)
addressE=Entry(root)
nameE.grid(row=0,column=1)
addressE.grid(row=1,column=1)
root.mainloop()

使用show参数隐藏输入的字符

其实Entry控件具有可以使用show参数设置隐藏输入字符的特性,所以也常被应用于
密码的输入控制。

样例:当输入密码时所输入的字符将隐藏并用"*"字符显示。

from tkinter import *
root=Tk()
root.title("ch5_1")
''
accountL=Label(root,text="Name ")
accountL.grid(row=0)
pwdL=Label(root,text="Address")
pwdL.grid(row=1)
accountL=Entry(root)
pwdL=Entry(root,show="*")
accountL.grid(row=0,column=1)
pwdL.grid(row=1,column=1)
root.mainloop()

Entry的get()方法

Entry有一个get()方法,可以利用这个方法获得目前Entry的字符串内容。
Widget控件有一个常用方法Quit,执行此方法时Python Shell窗口的程序将
结束,但是此窗口应用程序继续运行。

from tkinter import *

def printInfo():
    print("Account:%s\nPassword: %s" % (accountE.get(),pwdE.get()))

root=Tk()
root.title("ch5_4")

msg="欢迎进入Silicon Stone Education系统"
logo=Label(root,text=msg)
accountL=Label(root,text="Name ")
accountL.grid(row=1)
pwdL=Label(root,text="Address")
pwdL.grid(row=2)
logo.grid(row=0,column=0,columnspan=2,pady=10,padx=10)
accountE=Entry(root)
pwdE=Entry(root,show="*")
accountE.grid(row=1,column=1)
pwdE.grid(row=2,column=1,pady=10)

loginbtn=Button(root,text="Login",command=printInfo)

loginbtn.grid(row=3,column=0)

quitbtn=Button(root,text="Quit",command=root.quit)
quitbtn.grid(row=3,column=1,pady=5)

root.mainloop()

Entry的insert()方法

在设计GUI程序时,常常需要在建立Entry的文本框默默建立输入文字,在Widget的Entry控件中
可以使用insert(index,s)方法插入字符串,其中,s是所插入的字符串,字符串会插入在index位置。
设计程序时可以使用这个方法为文本框建立默认的文字,通常会将它放在Entry()方法建立完文本框
后。

from tkinter import *

def printInfo():
    print("Account:%s\nPassword: %s" % (accountE.get(),pwdE.get()))

root=Tk()
root.title("ch5_4")

msg="欢迎进入Silicon Stone Education系统"
logo=Label(root,text=msg)
accountL=Label(root,text="Name ")
accountL.grid(row=1)
pwdL=Label(root,text="Address")
pwdL.grid(row=2)
logo.grid(row=0,column=0,columnspan=2,pady=10,padx=10)
accountE=Entry(root)
pwdE=Entry(root,show="*")
accountE.insert(0,"Kevin")
pwdE.insert(0,"pwd")
accountE.grid(row=1,column=1)
pwdE.grid(row=2,column=1,pady=10)

loginbtn=Button(root,text="Login",command=printInfo)

loginbtn.grid(row=3,column=0)

quitbtn=Button(root,text="Quit",command=root.quit)
quitbtn.grid(row=3,column=1,pady=5)

root.mainloop()

Entry的delete()方法

在tkinter模块的应用中可以使用delete(first,last=None)方法删除Entry内的从第first字符
到last-1字符间的字符串,如果要删除整个字符串可以使用delete(0,END)

样例:当单击Login按钮后,清空文本框Entry中的内容。

from tkinter import *

def printInfo():
    print("Account:%s\nPassword: %s" % (accountE.get(),pwdE.get()))
    accountE.delete(0,END)
    pwdE.delete(0,END)

root=Tk()
root.title("ch5_7")

msg="欢迎进入Silicon Stone Education系统"
logo=Label(root,text=msg)
accountL=Label(root,text="Name ")
accountL.grid(row=1)
pwdL=Label(root,text="Address")
pwdL.grid(row=2)
logo.grid(row=0,column=0,columnspan=2,pady=10,padx=10)
accountE=Entry(root)
pwdE=Entry(root,show="*")
accountE.insert(0,"Kevin")
pwdE.insert(0,"pwd")
accountE.grid(row=1,column=1)
pwdE.grid(row=2,column=1,pady=10)

loginbtn=Button(root,text="Login",command=printInfo)

loginbtn.grid(row=3,column=0)

quitbtn=Button(root,text="Quit",command=root.quit)
quitbtn.grid(row=3,column=1,pady=5)

root.mainloop()

计算数学表达式使用eval()

Python内有一个非常好用的计算数学表达式的函数eval,该函数可以直接传回此数学表达式的
计算结果。它的语法格式如下:
result=eval(expreesion)

上述计算结果也是用字符串传回。

样例:在Entry内输入数学表达式,本程序会列出结果。

from tkinter import *

def cal():
    out.configure(text="结果:"+str(eval(equ.get())))

root=Tk()
root.title("ch5_9")

label=Label(root,text="请输入数学表达式:")
label.pack()
equ=Entry(root)
equ.pack(pady=5)
out=Label(root)
out.pack()
btn=Button(root,text="计算",command=cal)
btn.pack(pady=5)
root.mainloop()

你可能感兴趣的:(五、文本框Entry)