About tkinter
Tkinter is a window design module using python. The Tkinter module ("Tk interface") is the interface to Python's standard Tk GUI toolkit. As a specific GUI interface of python, it is an image window. Tkinter is a GUI interface of python, which can be edited. We can use GUI to realize many intuitive functions. For example, if we want to develop a calculator, if it is just a program input and output window, it does not use the user experience. It is necessary to develop a small image window.
tkinter control details:
tkinter parts table: The 16 core Windows supported by tkinter are as follows:Button: a simple button used to execute a command or other operation.
Canvas: organize graphics. This component can be used to draw charts and graphs, create graph editors, and implement custom widgets.
Checkbutton: represents a variable with two different values. Clicking this button will switch between the two values.
Entry: text entry field.
Frame: a container widget. Frames can have borders and backgrounds. When an application or dialog layout is created, frames are used to organize other widgets.
Label: displays a text or image.
Listbox: displays a list of options. Listbox can be configured to get the behavior of radiobutton or checklist.
Menu: menu bar. Used to implement pull-down and pop-up menus.
Menubutton: menu button. Used to implement pull-down menus.
Message: displays a text. Similar to a label widget, but can automatically adjust text to a given width or ratio.
RadioButton: represents a variable that can have one of multiple values. Clicking on it will set the value for this variable and clear any other RadioButton associated with this same variable.
Scale: allows you to set a numeric value through the slider.
Scroll bar: standard scroll bar for use with canvas, entry, listbox, and text widgets.
Text: format text display. Allows you to display and edit text with different styles and properties. Supports both embedded images and windows.
Toplevel: a container widget that appears as a separate, topmost window.
Message box: a message box that displays the message box for your application. (tkmessagebox in Python 2)
Note that widget classes are not graded in Tkinter; all widget classes are siblings in the tree.
Some feature code examples:
1. Creation and use of main window and Lable label:#Import tkinter package import tkinter window=tkinter.Tk()#Create a window object window.geometry("500x300")#Define window size window.title("My Window")#The name of the window # Add a text label to the window, using tkinter.label to add lable=tkinter.Label(text='Hello!this is Tkinter', bg='red', font=('Imitation song', 15), width=30, height=2) #bg is the background, font is the font, width is the length, and height is the height. The length and height here are the length and height of the characters. For example, height=2, which means the label has two characters high #Of course, if you just want the characters to appear in the window without setting the background and font, you can write as follows: # lable=thinter.Lable("Hello!this is Tkinter") lable.pack()#Lock text label window.mainloop()
The operation results are as follows:
Brief description of Button widget:
The Button widget is a standard Tkinter widget that implements various buttons. Buttons can contain text or images, and you can associate a button with a Python function or method. When this button is pressed, Tkinter automatically calls the associated function or method.
The button can display only one font, but the text can span lines. In addition, a letter in this text can be underlined, for example, to indicate a shortcut key. By default, the Tab key is used to move the focus to a button part.
In short, the Button widget is used to let the user say "perform this task for me now". Usually we use the text or image displayed on the button to prompt. Buttons are usually used in toolbars or application windows, and to receive or ignore data entered in dialog boxes. For the matching of buttons and input data, refer to the Checkbutton and Radiobutton components.
Button part usage syntax:
btn=tkinter.Button(window,text = "OK", command = callback function name)
#Import tkinter package import tkinter window=tkinter.Tk()#Create a window object window.geometry("500x300")#Define window size window.title("My Window")#The name of the window # Set the text variable in the label to character type and receive it with var var=tkinter.StringVar() # Add a text label in the window, and use tkinter.label to add it. However, if the text in the window is in the form of a text variable, what value to assign depends on what var is lable=tkinter.Label(textvariable=var, bg='red', font=('Imitation song', 15), width=30, height=2) #bg is the background, font is the font, width is the length, and height is the height. The length and height here are the length and height of the characters. For example, height=2, which means the label has two characters high lable.pack()#Lock text label def hit(): var.set("hello!this is tkinter") #Define button btn=tkinter.Button(text="Determine",font=('Arial', 12), width=10, height=1,command=hit) btn.pack() window.mainloop()
The operation results are as follows:
Entry is a single line text input field provided in tkinter class, which is used to input and display one line of text
import tkinter window=tkinter.Tk()#Create a window object window.geometry("500x300")#Define window size window.title("My Window")#The name of the window h1=tkinter.Entry(window,show="*",font=('Arial',20))#Show in ciphertext h1.pack() h2=tkinter.Entry(window,show=None,font=('Arial',20))#Display in clear text h2.pack() window.mainloop()
The operation results are as follows:
#1. Import tkinter package import tkinter #2. Define the basic information of the window window=tkinter.Tk()#Create a window object window.geometry("500x300")#Define window size window.title("My Window")#The name of the window var1=tkinter.StringVar() #3. Add the label of "lable" in the window to display the selected option and receive it with var1 lable1=tkinter.Label(window,textvariable=var1,background='yellow',fg='red',font=('Arial',20),width=10,height=2) lable1.pack() #5. Create a method for button click event def btn_hit(): value=listbox.get(listbox.curselection())#Get the currently selected text var1.set(value) #4. Create a button btn=tkinter.Button(window,text="print selection",width=15,height=2,bg='green',command=btn_hit) #Note that the function after command cannot be added (), otherwise, the function will be executed without clicking the button btn.pack() #6. Create a listbox and add content to it var2=tkinter.StringVar() var2.set((1,2,3,4,5,6))#Set value for var2 # 6.1 creating listbox listbox=tkinter.Listbox(window,listvariable=var2)#Assign the value of var2 to listbox #6.2 create a list and loop the values in the list into listbox list=[7,8,9] for item in list: listbox.insert(item) #Don't forget to lock listbox listbox.pack() window.mainloop()
The operation results are as follows:
Message box: the message box used to display application messages is actually equivalent to the pop-up window (prompt dialog box) we usually see,
Usage: you need to define a trigger function to trigger this pop-up box. Here you can use the button button function to trigger the pop-up box, and call the messageBox through the trigger function. Click button to display the dialog box.The code is as follows:
#1. Import tkinter package import tkinter import tkinter.messagebox #2. Define the basic information of the window window=tkinter.Tk()#Create a window object window.geometry("500x300")#Define window size window.title("My Window")#The name of the window #3. Create a method for button click events def btn_hit(): tkinter.messagebox.showinfo(title="hi",message='Hello!')#Use the showinfo prompt dialog box # tkinter.messagebox.showerror(title="hi",message="Error")#Use showerror to prompt the error dialog # tkinter.messagebox.showwarning(title="hi",message='Warning!')#Use showwarning to prompt the warning dialog box # print(tkinter.messagebox.askquestion(title="hi",message="Hello!"))#Use askquestion to prompt the selection dialog box. The selection content is yes / No # print(tkinter.messagebox.askyesno(title="hi",message="Hello!"))#Use askyesno to prompt the selection dialog box and return true false #4. Create a button btn=tkinter.Button(window,text="btn_hit",width=15,height=2,bg='green',command=btn_hit) #Note that the function after command cannot be added (), otherwise, the function will be executed without clicking the button btn.pack() window.mainloop()
The operation results are as follows:
Click hello to return to yes, click No to return to no (corresponding to show question)
Click Yes to return true, click No to return false, corresponding to askyesno