Entry single line text box
grammar
Entry(parent object, options,...)
Common attributes
Note: This picture is from the rookie tutorial.
Common method
1.show hide input parameters
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
self.name=Label(self,text="账号").grid(row=0,column=0)
self.password=Label(self,text="密码").grid(row=1,column=0)
self.name_entry=Entry(self).grid(row=0,column=1,padx=5,pady=5)
self.password_entry=Entry(self).grid(row=1,column=1)
if __name__ == '__main__':
root=Tk()
root.geometry('300x200+200+200')
root.title('萤火虫')
root.iconbitmap('1.ico')
app=Application(master=root)
root.mainloop()
Enter the account password.
You will find that the password is displayed in this way. It is not safe. If you want to display it in cipher text, use the show parameter to hide the input characters.
Modify the code:
self.password_entry=Entry(self,show="*").grid(row=1,column=1)
That's much better.
I found that it can still be like this:
just modify the show parameters.
2. Use of get method.
from tkinter import *
from tkinter import messagebox
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
self.name=Label(self,text="账号").grid(row=0,column=0)
self.password=Label(self,text="密码").grid(row=1,column=0)
v1 = StringVar()
self.name_entry=Entry(self,textvariable=v1)
self.name_entry.grid(row=0,column=1,padx=5,pady=10)
v2 = StringVar()
self.password_entry=Entry(self,textvariable=v2,show="*")#textvariable绑定组件
self.password_entry.grid(row=1,column=1)
self.login_system=Button(self,text="登录",width=7,command=self.login_system)
self.login_system.grid(row=2,column=1,sticky=W,padx=5,pady=10)
self.quit_system=Button(self,text="退出",width=7,command=self.quit_system)
self.quit_system.grid(row=2,column=1,sticky=E,padx=5,pady=10)
def login_system(self):
print("账号:"+self.name_entry.get())#get方法
print("密码:"+self.password_entry.get())
messagebox.showinfo("提示:","登录成功")
def quit_system(self):
root.quit()
if __name__ == '__main__':
root=Tk()
root.geometry('300x200+200+200')
root.title('萤火虫')
root.iconbitmap('1.ico')
app=Application(master=root)
root.mainloop()
Click the exit button to exit the program.
3. Use of the
insert method insert(index, s) index is the position to insert, and s is the content to be inserted.
Add these two lines of code.
self.name_entry.insert(0,"123456789")
self.password_entry.insert(0,"1111")
def createWidget(self):
self.name=Label(self,text="账号").grid(row=0,column=0)
self.password=Label(self,text="密码").grid(row=1,column=0)
v1 = StringVar()
self.name_entry=Entry(self,textvariable=v1)
self.name_entry.grid(row=0,column=1,padx=5,pady=10)
self.name_entry.insert(0,"123456789")
v2 = StringVar()
self.password_entry=Entry(self,textvariable=v2,show="*")
self.password_entry.grid(row=1,column=1)
self.password_entry.insert(0,"1111")
self.login_system=Button(self,text="登录",width=7,command=self.login_system)
self.login_system.grid(row=2,column=1,sticky=W,padx=5,pady=10)
self.quit_system=Button(self,text="退出",width=7,command=self.quit_system)
self.quit_system.grid(row=2,column=1,sticky=E,padx=5,pady=10)
The default account and password are automatically filled in.
4. Use of delete method.
delete(first,last)
def quit_system(self):
self.name_entry.delete(0,END)
self.password_entry.delete(0,END)
The account and password will be cleared when the logout button is clicked.
5.eval() method
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack()
self.createWidget()
def createWidget(self):
self.label=Label(self,text="输入数学表达式").grid(row=0,column=0)
v1 = StringVar()
self.entry=Entry(self,textvariable=v1)
self.entry.grid(row=0,column=1,padx=5,pady=10)
self.system=Button(self,text="开始计算",width=7,command=self.system)
self.system.grid(row=3,column=1,sticky=W,padx=5,pady=10)
def system(self):
self.out=Label(self,text="计算结果为:%s"%(str(eval(self.entry.get())))).grid(row=2)
if __name__ == '__main__':
root=Tk()
root.geometry('300x200+200+200')
root.title('萤火虫')
root.iconbitmap('1.ico')
app=Application(master=root)
root.mainloop()
Click Start calculation to
get the result.