This is the 30th 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:

Our last installment focused on the Grid layout manager, which arranges components in a Grid format

In contrast to Grid, this installment will learn another way to use a simpler layout manager, Pack, related properties, and usage

⏰ rub your hands, cheer up, open our study today, Let’s go~

1. Overview of Pack Layout Manager

The Pack Layout Manager is Tkinter’s way of managing the layout of components in a main window, creating floating, auto-scaling extensions that make it easier and more convenient to use the Pack Layout manager on a small number of components.

Pack layout manager features

  • Pack adds children to parent components in the order they were created, arranged naturally vertically or horizontally.
  • Pack adds components from the top down in the parent component by default if no options are specified
  • Pack is minimal and can be used to quickly build interfaces

Scenarios where Pack layout manager fits

  • Put small components into a window and fill it
  • Place small components at the top
  • Place multiple components side by side
  • Only suitable for simple vertical or horizontal arrangement

2. Pack common attributes

attribute role
side Specify docking direction. Optional values: left,top,right,bottom
fill Specifies the fill color. Optional values: x(horizontal fill), y(vertical fill), both (horizontal and vertical fill), none: no fill
expand The value can be True or False
anchor Anchor position, specify component position, optional: N(NORTH/down), E(east/right), S(South/down), W(West/left), CENTER(middle)
padx Margin in the x direction
pady Margin in the y direction
iPadx The inside margin in the x direction
ipady The inside margin in the y direction

📢 Important Note:

  1. Side is the most commonly used property for the pack method, and the sample figure with side as TOP is shown below

  2. An example figure with side LEFT is shown below

  3. Component exclusive space

  • The location of the component takes up the entire row or column of available space in the main window, depending on the location

  • side = TOP/BOTTOM

    (1) The component occupies the entire line

    (2) Subsequent components can only be placed below or above them

    (3) Components are arranged up and down

  • side = LEFT/RIGHT

    (1) The component occupies the entire column

    (2) Subsequent components can only be arranged to their left or right

    (3) Components are arranged left and right

3. Test the cat

The Pack layout manager exercise results in the following:

  1. As shown in the illustration above, there are two Fream containers. Create two containers F1 and F2, arranged vertically

    
    f1 = Frame(self)
    f1.pack()
    
    f2 = Frame(self)
    f2.pack()
    Copy the code
  2. Frame1 Contains five horizontal rows of keys

    • Five buttons are stored in a list
    • Use the for loop to create five Button objects each
    • Button object main window uses F1
    
    btlist = ("popular","chinese","Japanse","Hiap","soft")
    
    for txt in btlist:
    
        Button(f1,text = txt).pack(side = LEFT,padx = "10")
    Copy the code
  3. The Fream2 container holds the black and white Label components

    • Label The main window is F2
    • Create 20 Label objects using the for loop
    • The Label background color is controlled using an if else statement
    
    btlist = ("popular","chinese","Japanse","Hiap","soft")
    
    for txt in btlist:
    
        Button(f1,text = txt).pack(side = LEFT,padx = "10")
    Copy the code
  4. Using GUI object-oriented programming, the Application(Frame) method is used to create the instance object, the method for creating the component is encapsulated in the createWidgetPack() method, and the component is created using the constructor

  5. 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, Super ().__init__(master) self.master = master self.pack() self.createWidgetPack () def CreateWidgetpack(self): f1 = Frame(self) f1.pack() f2 = Frame(self) f2.pack() btlist = ("popular","chinese","Japanse","Hiap","soft") for txt in Btlist: Button(f1,text = TXT). Pack (side = LEFT,padx = "10") for I in range(1,20): Button(f1,text = TXT). Pack (side = LEFT,padx = "10") for I in range(1,20): Label(f2,width = 5,height = 10,borderwidth=1,relief = "solid",bg ="black"if i%2==0 else "white").pack(side ="left",padx =2) root = Tk() root.title("MyfirstAPP") app = Application(master=root) root.mainloop()Copy the code

conclusion

In this installment, we’ll learn about the Python Tkinter Layout Manager Pack

The Pack layout manager can quickly implement layout effects for simple widgets.

The above is the content of this issue, welcome big guys to point up comments and corrections, see you next time ~ღ(´ ᴗ · ‘) than heart 🌹🌹