Tkinter库的学习记录05-文本框Entry

📅 2026/7/11 1:37:05
Tkinter库的学习记录05-文本框Entry
5、文本框Entry5-1 文本框Entry的基本概念所谓的文本框Entry通常是指单行的文本框在GUI程序设计中这是用于输入的最基本的Widget控件我们可以使用它输入单行字符串如果所输入的字符串长度大于文本框的宽度所输入的文字会自动隐藏造成部分内容无法显示。碰到这种情况时可以使用箭头键移动鼠标光标到看不到的区域。需留意的是文本框Entry限定是单行文字如果想要处理多行文字需使用Widget控件中的Text后面会详解。Entry的使用格式如下。Entry(父对象, option, ...)参数一同上下面是Entry()方法内其它常用的option参数(1) bg或background背景色彩。(2) borderwidth或bd边界宽度默认是2像素。(3) command当用户更改内容时会自动执行此函数。(4) cursor当鼠标光标在复选框上时的光标形状、(5) exportselection如果执行选取时所选取的字符串会自动输出至剪贴板如果想要避免可以设置exportselection0.(6) fg或foreground前景色彩。(7) font字形(8) height高单位时字符高(9) highlightbackground当文本框取得焦点时背景颜色。(10) highlightcolor当文本框取得焦点时的颜色。(11) justify当含多行文字时最后一行的对齐方式。(12) relief默认是reliefFLAT可由此控制文字外框。(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, textName ) nameL.grid(row0) addressL Label(root, textAddress ) addressL.grid(row1) nameE Entry(root) addressE Entry(root) nameE.grid(row0, column1) addressE.grid(row1, column1) root.mainloop()运行结果上述代码中设置grid(row0)在没有设置columnx的情况下系统将自动设置column0。5-2 使用show参数隐藏输入的字符其实Entry控件具有可以使用show参数设置隐藏输入字符的特性所以也常被应用与密码的输入控制。下面程序中模拟登录界面有输入账号和密码当输入密码时所输入的字符将隐藏并用*字符显示。from tkinter import * root Tk() root.title(ch5_2) accountL Label(root, textAccount ) accountL.grid(row0) pwdL Label(root, textPassword ) pwdL.grid(row1) accountE Entry(root) pwdE Entry(root, show*) accountE.grid(row0, column1) pwdE.grid(row1, column1) root.mainloop()运行结果下面代码建立一个公司网页登录界面。from tkinter import * root Tk() root.title(ch5_3) msg 欢迎进入Silicon Stone Education系统 Png PhotoImage(filetubiao.png) logo Label(root, imagePng, textmsg, compoundBOTTOM) accountL Label(root, textAccount) accountL.grid(row1) pwdL Label(root, textPassword ) pwdL.grid(row2) logo.grid(row0, column0, columnspan2, padx10, pady11) accountE Entry(root) pwdE Entry(root, show*) accountE.grid(row1, column1) pwdE.grid(row2, column1, padx10) root.mainloop()执行结果5-3 Entry的get()方法Entry有一个get()方法可以利用这个方法获得目前Entry的字符串内容。Widget控件有一个常用方法Quit执行此方法时Python Shell窗口的程序将结束但是此窗口应用程序继续运行。扩展上面网页的登录页面增加Login和Quit功能按钮。如果单击Login功能按钮在Python Shell中将列出所输入的Account和Password若是单击Quit按钮则Python Shell窗口执行结束但是屏幕上仍可以看到此窗口在执行。from tkinter import * def printInfo(): print(Account: , accountE.get()) print(Password: , pwdE.get()) root Tk() root.title(ch5_4) msg 欢迎进入Silicon Stone Education系统 Png PhotoImage(filetubiao.png) logo Label(root, imagePng, textmsg, compoundBOTTOM) accountL Label(root, textAccount) accountL.grid(row1) pwdL Label(root, textPassword ) pwdL.grid(row2) logo.grid(row0, column0, columnspan2, padx10, pady11) accountE Entry(root) pwdE Entry(root, show*) accountE.grid(row1, column1) pwdE.grid(row2, column1, padx10) # 以下建立Login和Quit按钮 loginbtn Button(root, textLogin, commandprintInfo) loginbtn.grid(row3, column0) quitbtn Button(root, textQuit, commandroot.quit) quitbtn.grid(row3, column1) root.mainloop()运行结果从上述结果可以看到Login按钮和Quit按钮并没有对齐上方的标签和文本框我们可以在grid()方法内增加sticky参数同时将此参数设为W即可靠左对齐字段。下面使用stickyW参数和padx10参数重新设计上面代码。from tkinter import * def printInfo(): print(Account: , accountE.get()) print(Password: , pwdE.get()) root Tk() root.title(ch5_4) msg 欢迎进入Silicon Stone Education系统 Png PhotoImage(filetubiao.png) logo Label(root, imagePng, textmsg, compoundBOTTOM) accountL Label(root, textAccount) accountL.grid(row1) pwdL Label(root, textPassword ) pwdL.grid(row2) logo.grid(row0, column0, columnspan2, padx10, pady10) accountE Entry(root) pwdE Entry(root, show*) accountE.grid(row1, column1) pwdE.grid(row2, column1, padx10) # 以下建立Login和Quit按钮 loginbtn Button(root, textLogin, commandprintInfo) loginbtn.grid(row3, column0, stickyW, padx5) quitbtn Button(root, textQuit, commandroot.quit) quitbtn.grid(row3, column1, stickyW, padx10) root.mainloop()运行结果5-4 Entry的insert()方法在设计GUI程序时常常需要在建立Entry的文本框内默认建立输入文字在Widget的Entry控件中可以使用insert(index, s)方法插入字符串其中s时所插入的字符串字符串会插入在index位置。设计程序时可以使用这个方法为文本框建立默认的文字通常会将它放在Entry()方法建立完文本框后。下面程序为Account文本框建立默认文字为Kevin为password文本框建立默认文字为pwdaccountE Entry(root) pwdE Entry(root, show*) accountE.insert(0, Kevin) pwdE.insert(0, 123)运行结果5-5 Entry的delete()方法在tkinter模块的应用中可以使用delete(first, lastNone)方法删除Entry内的从第first字符到第last-1字符间的字符串如果要删除整个字符串可以使用delete(0, END)。下面程序扩展了上面的程序当单击Login按钮后清空文本框Entry中的内容def printInfo(): print(Account: , accountE.get()) print(Password: , pwdE.get()) accountE.delete(0, END) pwdE.delete(0, END)运行结果5-6 计算数学表达式使用eval()Python内有一个非常好用的计算数学表达式的函数eval该函数可以直接传回此数学表达式的计算结果。它的语法如下。result eval(expression) # express 是字符串上述计算结果也是用字符串传回。下面代码中输入数学表达式本程序会传回执行结果expression input(请输入数学表达式: ) print(结果是: , eval(expression))运行结果PS E:\code\python\GuiTkinter\ch5 python .\ch5_1.py 请输入数学表达式: 9*108 结果是: 98 PS E:\code\python\GuiTkinter\ch5了解了eval()函数的用法后可以将上述程序改为GUI程序。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(pady5) out Label(root) out.pack() btn Button(root, text计算, commandcal) btn.pack(pady5) root.mainloop()运行结果