We learned about windows and labels earlier. If we want to place many controls on a window, we cannot manage them well just by relying on positioning, so we need to use a container that can store management controls in the window - Frame .
The use of Frame is similar to that of windows. Let’s learn it directly through examples:
importtkinterdefmain():win=tkinter.Tk()win.minsize(366,366)#Minimum size win.maxsize(888,888)#Maximum size frame_one=tkinter.Frame(win)#Use Frame control area_one=tkinter.Label(frame_one,text ='dotcpp',font=(Chinese regular script, 20),fg=red)area_one.pack(side='top')#Put it on top area_two=tkinter.Label(frame_one,text='Python',font=(黑体,20),fg=blue)area_two.pack(side='bottom')#Put it below frame_one.pack(side='left')#Put it on the left side#The container on the left is above the dividing line, and the container on the right is below frame_two=tkinter.Frame(win)area_one=tkinter.Label(frame_two,text='dotcpp',font=(Chinese regular script,20),fg=red)area_one.pack(side='top')#Put it on area_two =tkinter.Label(frame_two,text='Python',font=(黑体,20),fg=blue)area_two.pack(side='bottom')#Put it below frame_two.pack(side='right')# Put it on the right win.mainloop()if__name__=='__main__':main()
The running result is:
You can use the following figure to understand the role of Frame in the window:
When we develop graphical interfaces, buttons are an indispensable item. The function of a button is to trigger an event once clicked. We can trigger the event through the button binding function.
First, let’s take a look at the relevant options in the Buton control through the following table.
When we use the Button control, we often use it together with the messagebox module in the tkinter module. The message box pops up through the latter. See the following code:
importtkinterimporttkinter.messageboxwin=tkinter.Tk()win.minsize(166,40)#Minimum size defbutton_event():tkinter.messagebox.showinfo(Button event, welcome to Python teaching) button_one=tkinter.Button(win, text=www. dotcpp.com,command=button_event)button_one.pack()win.mainloop()
After running, the following window will appear.
Click this button and the following window will appear.
Frame and Button are indispensable controls when we create graphical interfaces. We will not introduce them too much here. The related methods of Button mentioned above can be introduced when you develop your own projects to make your own The interface is more beautiful.