In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "how Tkinter builds grid layout manager". The explanation in this article is simple and clear, easy to learn and understand. Please follow the ideas of Xiaobian and go deep into it slowly to study and learn "how Tkinter builds grid layout manager" together!
The pack layout manager was a common layout method used in early Tkinter. This approach is easy for simple situations, but as the requirements become more complex, the difficulty of layout increases rapidly. This article introduces another approach: grid layout manager.
The English word grid means grid. As the name suggests, grid layout manager works by imagining dividing the parent window space into a grid and laying out controls in the grid.
For example, the following small window can be regarded as a grid with three rows and two columns.
This image can be constructed using the following code:
from tkinter import *root = Tk()Label (root, text="username").grid (row=0, sticky=W)Label (root, text="password").grid (row=1, sticky=W)Entry(root).grid (row=0, column=1, sticky=E)Entry(root).grid (row=1, column=1, sticky=E)Button(root, text="Cancel").grid(row=2, column=0, sticky=E)Button(root, text="Login").grid(row=2, column=1, sticky=E)root.mainloop()
As you can see from the code, the grid method uses three parameters:
row: Specifies the row where the control is located, default is the next row
column: Specifies the column in which the control is located, default is 0
sticky: Optional values include NW, N, NE, W, E, SW, S, SE. These values can be interpreted as directions on a map, e.g. NE is northeast. The following diagram shows where each value is placed in the layout grid.
These values can also be combined, for example E+W has the effect of stretching space horizontally. When we modify the code that generates the Login button as follows,
Button(root, text="Login").grid(row=2, column=1, sticky=E+W)
You can get the following layout, the Login button has been covered with grid (2, 1):
If you feel that this layout is not beautiful enough, you can also consider the following layout:
We want each edit box to occupy two grids horizontally. This requires the following parameters:
columnspan: Specifies the number of grids occupied by the control in the horizontal direction.
rowspan: Specifies the number of grids the control occupies vertically.
The code is as follows:
from tkinter import *root = Tk()Label (root, text="username").grid (row=0, sticky=W)Label (root, text="password").grid (row=1, sticky=W)Entry(root).grid (row=0, column=1, sticky=E, columnspan=2)Entry(root).grid (row=1, column=1, sticky=E, columnspan=2)Button (root, text="Cancel").grid(row=2, column=1, sticky=E+W)Button(root, text="Login").grid(row=2, column=2, sticky=E+W)root.mainloop()
In addition to specifying a columnspan value of 2 for the edit box, the code specifies sticky values of E+W for both buttons to fill the grid they are on.
Overall grid layout manager, simple and easy to understand, flexible to use, is currently recommended by Tkinter layout.
Thank you for reading, the above is "Tkinter how to build grid layout manager" content, after the study of this article, I believe we have a deeper understanding of how to build grid layout manager Tkinter, the specific use of the situation still needs to be verified. Here is, Xiaobian will push more articles related to knowledge points for everyone, welcome to pay attention!
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.