[Python Basics] geometric manager of Tkinter, pythontkinter
Tkinter supports three geometric managers: Grid manager, package manager, and location manager.
Tip: Since each manager has its own style of placing small components, it is best not to use multiple managers in the same container. You can use the framework as a sub-container to obtain the expected layout.
1. Grid Manager
# Geometric Manager (1) ----- grid manager ''' the grid manager places small components in each unit of an invisible grid. You can place a widget in a specific row or column, or you can use the rowspan and columnspan parameters to place the widget in multiple rows and columns. ''' From tkinter import * class GridManagerDemo: window = Tk () window. title ("Grid Manager Demo") message = Message (window, text = "This Message widget occupies three rows and two columns") message. grid (row = 1, column = 1, rowspan = 3, columnspan = 2) Label (window, text = "First Name :"). grid (row = 1, column = 3) Entry (window ). grid (row = 1, column = 4, padx = 5, pady = 5) Label (window, text = "Last Name :"). grid (row = 2, column = 3) Entry (window ). grid (row = 2, column = 4) Button (window, text = "Get Name "). grid (row = 3, padx = 5, pady = 5, column = 4, sticky = E) window. mainloop () GridManagerDemo ()
2. Package Manager
# Geometric Manager (2) ----- Package Manager ''' contains the manager that places one of the widgets on the top of the other or one by one. ''' # First from tkinter import * class PackManagerDemo: def _ init _ (self): window = Tk () window. title ("Pack Mananger Demo 1") Label (window, text = "Blue", bg = "blue "). pack () # fill uses X, Y, and BOTH to fill in the horizontal, vertical, or two-direction space # expand tells the manager to allocate additional space to the small component Label (window, text = "Red", bg = "red "). pack (fill = BOTH, expand = 1) Label (window, text = "Green", bg = "green "). pack (fill = BOTH) window. mainloop () PackManagerDemo () # Second cl Ass PackManagerDemoWithSide: window = Tk () window. title ("Pack Manager Demo 2") # side can be LEFT, RIGHT, TOP, and BOTTOM. The default value is TOP. Label (window, text = "Blue", bg = "blue "). pack (side = LEFT) Label (window, text = "Red", bg = "red "). pack (side = LEFT, fill = BOTH, expand = 1) Label (window, text = "Green", bg = "green "). pack (side = LEFT, fill = BOTH) window. mainloop () PackManagerDemoWithSide ()
3. Location Manager
# Geometric Manager (3) ----- Location Manager ''' the location manager places the widget in an absolute position. '''From tkinter import * class PlaceManagerDemo: def _ init _ (self): window = Tk () window. title ("Place Manager Demo") Label (window, text = "Blue", bg = "blue "). place (x = 20, y = 20) Label (window, text = "Red", bg = "red "). place (x = 50, y = 50) Label (window, text = "Green", bg = "green "). place (x = 80, y = 80) window. mainloop () PlaceManagerDemo ()