In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains the usage of Tkinter pack layout manager. Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn the usage of "Tkinter pack layout manager"!
Take a look at the code from the previous article:
from tkinter import *#Build main window main = Tk()#Build label Label(main, text='Hello Tkinter! ').pack()#Build Exit Button(main, text ='Quit', command=main.quit).pack()#Execute main loop main.mainloop()
After building the Label and Button controls, the program calls the pack method twice to place them in the top-level window:
Although he didn't know why, the picture changed to the one above. If that's what you want, fine, but if you want to adjust the layout, say you want the text to be on the left side of the button, what do you do? Here is the answer:
#from tkinter import *import tkinter as tk#build main window main = tk.Tk()#build label tk.Label(main, text='Hello Tkinter! ').pack(side=LEFT, expand=YES, fill=X)#construct exit button tk.Button(main, text='Quit', command=main.quit).pack(side=LEFT, expand=NO)#execute main loop ()
After the above code is executed, the screen will look like this:
This involves laying out controls within windows. There are three ways Tkinter manages layouts. Today, I will explain the first one: pack.
The pack layout follows a first-come, first-served principle to arrange space for controls, and it has several common parameters.
The first is the side parameter, which together has 4 optional values: LEFT, TOP, RIGHT and BOTTOM. The meaning is where the child window needs to be placed in the parent window. LEFT and RIGHT belong to horizontal layout, TOP and BOTTOM belong to vertical layout. If the layout is continuous horizontally or vertically, different controls share space in the parent window. If a switch occurs between horizontal and vertical layouts, the switched layout takes up space for one of the controls in the pre-switch layout. For example, the following code:
Button(root, text="AAAAAAAAAAAAA").pack(side=LEFT)Button(root, text="AAAAAAAAAAAAA").pack(side=LEFT)Button(root, text="AAAAAAAAAAAAA").pack(side=TOP)Button(root, text="AAAAAAAAAAAAA").pack(side=TOP)
The resulting layout looks like this:
To avoid complications, we used the same text column for each button, so everything looks fine, but if we used different text columns, things would change, such as the following code:
Button(root, text="Cat").pack(side=LEFT)Button(root, text="Dog").pack(side=LEFT)Button(root, text="Tiger").pack(side=TOP)Button(root, text="Bear").pack(side=TOP)
The resulting image looks like this:
Because of the different text columns, the size of each button will be different, which is very ugly.
The first step in solving this problem requires another parameter: expand. It can be 1 or 0, Yes or no. When the value is 1 or Yes, it means that the button wants to occupy more space than the actual area. We can modify the code using the expand parameter as follows:
Button(root, text="Cat").pack(side=LEFT, expand=YES)Button(root, text="Dog").pack(side=LEFT, expand=YES)Button(root, text="Tiger").pack(side=TOP, expand=YES)Button(root, text="Bear").pack(side=TOP, expand=YES)
The resulting images are as follows:
Each button occupies almost the same amount of space in the screen, but the actual size of each button is not the same.
We can use another option fill to complete the control to fill its own space, which has three optional values: X,Y and BOTH, which respectively represent different filling directions. For example, we can use the following code to achieve horizontal filling:
Button(root, text="Cat").pack(side=LEFT, expand=YES, fill=X)Button(root, text="Dog").pack(side=LEFT, expand=YES, fill=X)Button(root, text="Tiger").pack(side=TOP, expand=YES, fill=X)Button(root, text="Bear").pack(side=TOP, expand=YES, fill=X)
You can get the following picture:
You can also use the BOTH option to achieve bi-directional filling:
Button(root, text="Cat").pack(side=LEFT, expand=YES, fill=BOTH)Button(root, text="Dog").pack(side=LEFT, expand=YES, fill=BOTH)Button(root, text="Tiger").pack(side=TOP, expand=YES, fill=BOTH)Button(root, text="Bear").pack(side=TOP, expand=YES, fill=BOTH)
The resulting images are as follows:
Simple layouts are convenient to use pack, but as the layout becomes more complex, the usage becomes more complex.
At this point, I believe that everyone has a deeper understanding of the "usage of Tkinter pack layout manager". Let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.