The function of the Entry control is to input text information on the keyboard. Its syntax format is as follows:
my_entry=Entry(container, optional)
The container is the location where the content is placed. The options are similar to the options in Button we learned in the previous section. The options are:
Delete all contents in first-last. If delete(0,END) is used, all contents in the input box will be deleted.
Get all the content in the input box.
Move the cursor to the position of the index parameter.
Returns the sequence number corresponding to the index parameter.
Insert the content in the text parameter into the position with index index.
Clear the text box.
Sets the horizontal scroll bar for text box links.
Let's use the Entry control through an example.
importtkinterwin=tkinter.Tk()Frame_one=tkinter.Frame(win)#First create a container to store the login Frame_one.pack(side='top')Frame_two=tkinter.Frame(win)#Create a container to store in the middle Password Frame_two.pack()Frame_three=tkinter.Frame(win)#Create another container below Store button Frame_three.pack(side='bottom')Label_one=tkinter.Label(Frame_one,text='Name:')Lable_two=tkinter.Label(Frame_two,text='Password:')Entry_one=tkinter.Entry(Frame_one, bd=5)Entry_two=tkinter.Entry(Frame_two,b d=5)Button_one=tkinter.Button(Frame_three,text='Login',activeforeground='red',activebackground='yellow',width='7')Button_one.pack(side='left')Button_two=tkinter. Button(Frame_three,text='Register',activeforegr ound='blue',activebackground='pink',width='7')Button_two.pack(side='right')Label_one.pack(side='left')Entry_one.pack(side='right')Lable_two. pack(side='left')Entry_two.pack(side='right')win.mainloop()
The output is:
We first placed three containers in the window, storing names, passwords and buttons from top to bottom. Then we placed an Entry control behind the name for us to enter the name, and placed an Enrty control behind the password to enter the password. Finally, put two buttons below to provide login and registration respectively. Finally, we put their positions on the left and right in one-to-one correspondence.
We then test the login information through function binding.
importtkinterastkimporttkinter.messageboxwin=tk.Tk()frame_name=tk.Frame(win)#Create a container to store the logged-in Label (text box) and Entry (input box) frame_name.pack(side=top)#Make the container on the page Top label_name=tk.Label(frame_nam e,text=YourName:)label_name.pack(side=left)entry_name=tk.Entry(frame_name,bd=5)entry_name.pack(side=right)#Create Label and Entry in the container, and put the label on the left. Entry is on the right # Same as below: frame_password=tk.Frame(w in)frame_password.pack()label_password=tk.Label(frame_password,text=YourPassword:)label_password.pack(side=left)entry_password=tk.Entry(frame_password,bd=5)entry_password.pac k(side=right)deflogin():ifentry_name.get()==qy:ifentry_password.get()==dotcpp:print(tkinter.messagebox.showinfo(login,Success!))else:print(tkinter.messagebox. showerror(login, Failed!))entry_name.delete(0,end)entry_password.delete(0,end)else:print(tkinter.messagebox.showerror(login,Failed!))entry_name.delete(0,end)entry_password.delete(0, end)defsi gnin():print(tkinter.messagebox.showerror(signin,WithoutCode!))#The function must be defined first, otherwise when you click the button to call the function, an error that the function does not exist will be reported frame_button=tk.Frame(win)#Create a container for storage Button frame_button.pack(side=bottom)# Make the container located at the bottom of the page but ton_login=tk.Button(frame_button,text=login,activeforeground=red,activebackground=yellow,width=7,command=login#command means to execute the defined function, and cannot execute the function that appears below (undefined function ))button_login.pack(side =left)#The same applies to the following: button_signin=tk.Button(frame_button,text=signin,activeforeground=blue,activebackground=pink,width=7,command=signin,)button_signin.pack(side=right)win.mainloop( )
The output interface is:
When we enter qy and dotcpp as follows:
If you enter other content, it will be displayed as follows: