This is the 31st day of my participation in the August Text Challenge.More challenges in August
Review and
Python Tkinter not only provides a variety of components, but also provides layout managers for managing components. There are three main types as follows:
- The Grid layout manager arranges components in a Grid manner
- The Pack layout manager arranges components vertically or horizontally
- Place layout manager, which can specify the size and location of components
In this installment, we’ll continue learning about the use of the third layout manager, Place-related property, provided by Python Tkinter
โฐ rub your hands, cheer up, open our study today, Let’s go~
1. Overview of Place Layout Manager
Python Tkinter provides the Place layout manager, which allows components to be arranged using absolute or relative positions.
Place Layout Manager features:
- The Place layout manager allows you to precisely control the location of components through coordinates
- The Place layout manager sort is ordered downward by default
- The Place layout manager can specify the size of the component
Place Usage scenario:
- The use of Place is flexible and suitable for complex multi-component layout
- When Place uses an absolute layout, the parameters of x and y need to be computed
- Place requires relx, RELY,relheight, and relwidth parameters
2. Place layout manager related properties
attribute | role |
---|---|
x | Non-negative, specifies the component’s horizontal offset position (pixels); When the RELX option is also specified, Place computes the RELx value first |
y | Non-negative, specifies the component’s vertical offset position (pixels); When you specify the RELY option, Place computes relx values first |
bordemode | Specifies the border mode. Optional values: INSIDE and OUTSIDE |
height | A non-negative integer that specifies the height of the component |
width | A non-negative integer that specifies the width of the component |
relwidth | Specifies the width of the component relative to the parent container. The value ranges from 0.0 to 1.0 |
relheight | Specifies the height of the component relative to the parent container. The value ranges from 0.0 to 1.0 |
relx | Specifies the horizontal position of the component relative to the parent container. The value ranges from 0.0 to 1.0 |
rely | Specifies the vertical position of the component relative to the parent container. The value ranges from 0.0 to 1.0 |
anchor | Align left with “W”, right with “E “, top with” N “, bottom with “S” |
in_ | Puts the component into the specified component. Specifies that the component must now be the parent component |
๐ข Important Note:
-
Place uses coordinates to manage components, with the X-axis extending to the right and the Y-axis extending down
(1) Absolute position: specify component coordinates by x and y, in unit of Pixel (pixel)
(2) Relative position: Use RELx and RELY to specify component coordinates, ranging from 0.0 to 1.0
(3) Through X, the larger the relX coordinate value is, the righter the position of the component is
(4) Use Y to specify that the larger the parameter value is, the lower the component position is
-
The Anchor property provides multiple values that can be used in combination
combination meaning NE On the top right SE On the lower right SW On the lower left corner NW On the top left N On the top E On the right S By below W On the left CENTER Center, default
3. Mapping table of Place, Grid and Pack attributes
The characteristics of | Place | pack | Grid |
---|---|---|---|
anchor | โ | โ | |
sticky | โ | ||
side | โ | ||
ipady | โ | โ | |
ipadx | โ | โ | |
pady | โ | โ | |
padx | โ | โ | |
x,y | โ | ||
relx,rely | โ | ||
fill | โ | ||
row | โ | ||
expand | โ | ||
column/columnspan | โ | ||
height/width | โ | ||
relheight/relwidth | โ | ||
in_ | โ | โ | โ |
๐ข Important Note:
-
About component location processing
(1) Pack and Place are controlled by Anchor
(2) Grid uses sticky to forward control components in the Grid
-
About component inline component handling
(1) Place, Pack, and Grid layout managers all support the IN_ attribute
-
About margin processing
(1) Place uses RELx and RELY relative position to realize the concept of inner margin, and X and Y realize the concept of outer margin
(2) Pack, Grid support ipadx, IPady to achieve the inner margin, PADx, PADy to achieve the outer margin
4. Test the cat
The Place layout manager exercise works like this:
-
According to the above analysis, first of all, each card is a Label component
- Create a Label component as puke1.gif
- Use the Place Layout manager to position components at (x=10,y=30)
self.photo =PhotoImage(file ="imgs/puke1.gif") self.puke1 =Label(self.master,image = self.photo) self.puke1.place(x=10,y=30) Copy the code
- Test the effect of a playing card
-
We can use the for loop derivation to create labels in batches
-
Place 10 cards in the imGS/directory
self.photos = [PhotoImage(file= "imgs/puke"+str(i+1)+".gif") for i in range(10)] self.pukes = [Label(self.master,image = self.photos[i]) for i in range(10)]Copy the code
-
-
Similarly, use the Place manager and for loop to batch manage the layout of 10 images
for i in range(10): self.pukes[i].place(x=10+i*40,y=30) Copy the code
-
We can use the event.widget.winfo_Geometry () method to obtain the coordinates and dimensions of the component
print(event.widget.winfo_geometry()) Copy the code
-
We can use the event.widget.winfo_y() method to get the component’s Y coordinate
print(event.widget.winfo_y()) Copy the code
-
We can add events for each Label by clicking on the Label component and moving it up
-
Use the event.widget.winfo_y() method to get the component y coordinate
-
If… Judging the else
-
Place moves the management component to different locations depending on the conditions
if event.widget.winfo_y()==30: event.widget.place(y=10) else: event.widget.place(y=30) Copy the code
-
-
Adds events to multiple groups of playing card label components
- For list-type data, bind_class can be used to bind events to its data
self.pukes[i].bind_class("Label","<Button-1>",self.choose) Copy the code
-
Using GUI object-oriented programming, the Application(Frame) is used to create the instance object, the method for creating the component is encapsulated in the CreateWidgetplace() method, and the constructor is used to create the component
-
The complete code is as follows:
from tkinter import * import random class Application(Frame): def __init__(self,master=None): # super() represents the definition of the parent class, Instead of superclass object super().__init__(master) self.master = master self.pack() self.createwidgetPlace () def CreateWidgetplace(self): # self.photo =PhotoImage(file ="imgs/puke1.gif") # self.puke1 =Label(self.master,image = self.photo) # self.puke1.place(x=10,y=30) self.photos = [PhotoImage(file= "imgs/puke"+str(i+1)+".gif") for i in range(10)] self.pukes = [Label(self.master,image = self.photos[i]) for i in range(10)] for i in range(10): self.pukes[i].place(x=10+i*40,y=30) self.pukes[i].bind_class("Label","<Button-1>",self.choose) def choose(self,event): print(event.widget.winfo_geometry()) print(event.widget.winfo_y()) if event.widget.winfo_y()==30: event.widget.place(y=10) else: event.widget.place(y=30) root = Tk() root["background"] = "white" root.geometry('600x270+200+300') root.title("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code
conclusion
In this installment, we’ll learn about using Python’s Tkinter layout manager, Place
Compared with the other two layout managers, Place uses more flexible scenes to create beautiful interface effects.
But for simple scenarios, places are more complex than Pack or Grid, so use them as needed.
The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~แฆ(ยด แด ยท ‘) than heart ๐น๐น