Python graphical interface basics: using wrappers (Pack) to lay out elements

introduction

In this course on the basics of Python graphical interfaces, we will delve into one of the layout managers of the Tkinter library: the packer ( Pack ) layout. Pack layout is a simple yet effective way to arrange and layout GUI elements in a Tkinter application . It allows you to stack elements together in one direction, which is useful for creating vertical or horizontal arrangement of elements. In this article, we will explain in detail how to use the Pack layout manager, including creating, configuring and positioning GUI elements.

What is Tkinter's Pack layout?

Tkinter is a GUI toolkit in the Python standard library for creating graphical user interface ( GUI ) applications. Tkinter provides multiple layout managers, Pack layout is one of them. Pack layout allows you to arrange elements, called controls, in a container along one direction (vertically or horizontally).

The main concepts of Pack layout include:

  • Container: Pack layout requires a container, usually a Frame or window. The element will be placed inside this container.

  • Direction: You can specify the direction in which the elements are arranged, either vertically or horizontally. By default, Pack layout is vertical, that is, elements are arranged from top to bottom.

  • Positioning: You can use Pack layout options to control the position of elements in the container, such as alignment, padding, etc.

Now let's start learning how to use Pack layout in Tkinter .

Step 1: Import the Tkinter module

First, make sure you have Python installed and include the Tkinter library. Then, import the Tkinter module in your Python script to use the functionality of the Tkinter library.

import tkinter as tk

Step 2: Create Tkinter window

Before using Tkinter , you need to create a Tkinter window object, usually called root. This window will serve as the main window of the GUI application.

root = tk.Tk()
root.title("Pack布局示例")

In the code above, we create a Tkinter window object rootand set the title of the window to " Pack Layout Example".

Step 3: Create and use Pack layout

To use Pack layout, you first need to create a container (usually a Frame ) to place the GUI elements. Then, pack()you add them to the container by calling the control's method and specify how they are arranged within the container.

Here is an example of how to create a Frame container and then arrange three buttons using Pack layout inside it:

# 创建一个Frame作为容器
frame = tk.Frame(root)
frame.pack()  # 使用Pack布局将容器添加到窗口中

# 创建三个按钮并使用Pack布局排列它们
button1 = tk.Button(frame, text="按钮1")
button1.pack()

button2 = tk.Button(frame, text="按钮2")
button2.pack()

button3 = tk.Button(frame, text="按钮3")
button3.pack()

In the above example, we first created a Frame container frameand then pack()added it to rootthe window using the method. Next, we created three buttons button1, button2and button3, and pack()arranged them using the method.

Step 4: Pack Layout Options

Pack layout allows you to use options to customize how elements are arranged within the container. Here are some commonly used Pack layout options:

  • side : Specifies the arrangement direction of the elements, which can be " top " (default value), " bottom ", " left " or " right ".

  • fill : Specifies whether the element fills the entire available space, which can be " none " (default), " x ", " y ", or " both ".

  • expand : Specifies whether the element expands to fill available space, can be True or False .

  • padx and pady : Specify the outer padding around the element.

  • ipadx and ipady : Specifies the inner padding around the element.

Here's an example of how to use Pack layout options to customize how buttons are arranged:

# 创建一个按钮并使用Pack布局选项
custom_button = tk.Button(frame, text="自定义按钮", padx=10, pady=5)
custom_button.pack(side="left", fill="both", expand=True)

In the example above, we created a custom button custom_buttonand used the Pack layout options to define the button's outer padding, alignment direction, whether to fill the available space, and whether to expand to fill the available space.

Complete sample code

Here is a complete sample code that demonstrates how to create a Tkinter window and then use Pack layout to arrange the buttons:

import tkinter as tk

# 创建Tkinter窗口
root = tk.Tk()
root.title("Pack布局示例")

# 创建一个Frame作为容器
frame = tk.Frame(root)
frame.pack()  # 使用Pack布局将容器添加到窗口中

# 创建三个按钮并使用Pack布局排列它们
button1 = tk.Button(frame, text="按钮1")
button1.pack()

button2 = tk.Button(frame, text="按钮2")
button2.pack()

button3 = tk.Button(frame, text="按钮3")
button3.pack()

# 创建一个自定义按钮并使用Pack布局选项自定义排列方式
custom_button = tk.Button(frame, text="自定义按钮", padx=10, pady=5)
custom_button.pack(side="left", fill="both", expand=True)

# 启动Tkinter主事件循环
root.mainloop()

Rendering:
Insert image description here

Code explanation

Let us explain the above code line by line:

  • We first imported the Tkinter module in order to use the functionality of the Tkinter library.

  • Created a Tkinter window object rootand set the title of the window to " Pack Layout Example".

  • A Frame container is created frameand then pack()added to rootthe window using the method.

  • Created three buttons button1, button2and button3, and arranged them using pack()the method.

  • Created a custom button custom_buttonand used Pack layout options to customize the arrangement, including padding, expansion, etc.

  • Finally, Tkinter 's main event loop is started , making the window interactive.

in conclusion

In this article, we learned how to arrange and layout GUI elements using Pack layout in Tkinter . Pack Layout is a simple yet powerful layout manager suitable for the arrangement of elements in many GUI applications. By creating a container and using the method, you can easily control how elements are arranged and use options to customize the layout of elements. Continuing to learn Tkinter , you will be able to create more complex and attractive user interfaces to meet the needs of different applications.pack()

Guess you like

Origin blog.csdn.net/qq_38161040/article/details/132857572
Recommended