[파이썬] `tkinter`의 geometry managers (`pack`, `grid`, `place`)

If you’re familiar with tkinter, the popular GUI toolkit for Python, you probably know that it provides different geometry managers to control the placement and layout of widgets within a window. In this blog post, we will explore three of the most commonly used geometry managers in tkinter: pack, grid, and place.

The pack manager

The pack manager is the simplest and most commonly used geometry manager in tkinter. It allows you to place widgets in either a horizontal or vertical manner, automatically adjusting their positions and sizes.

To use the pack manager, you simply call the pack() method on a widget. The widgets are then placed one after another, based on the order in which pack() was called. By default, widgets are vertically stacked.

Example:

from tkinter import *

root = Tk()

frame = Frame(root)
frame.pack()

label1 = Label(frame, text="Label 1")
label1.pack()

label2 = Label(frame, text="Label 2")
label2.pack()

root.mainloop()

In the example above, we create a Frame widget, and then two Label widgets inside the frame. The Label widgets are automatically placed one below the other within the Frame using the pack manager.

The grid manager

The grid manager allows you to place widgets in a table-like structure, specifying their rows and columns. This offers more control over the placement compared to the pack manager.

To use the grid manager, you call the grid() method on a widget and specify the row and column index where you want it to be placed.

Example:

from tkinter import *

root = Tk()

label1 = Label(root, text="Label 1")
label1.grid(row=0, column=0)

label2 = Label(root, text="Label 2")
label2.grid(row=0, column=1)

root.mainloop()

In the example above, we create two Label widgets and use the grid manager to place them in a 1x2 grid. The first label is placed in the first row and first column, while the second label is placed in the first row and second column.

The place manager

The place manager allows you to manually specify the position and size of widgets within a window. This offers the highest level of control but can be more complex to use compared to the other managers.

To use the place manager, you call the place() method on a widget and specify the x and y coordinates where you want it to be placed.

Example:

from tkinter import *

root = Tk()

label1 = Label(root, text="Label 1")
label1.place(x=50, y=50)

label2 = Label(root, text="Label 2")
label2.place(x=100, y=100)

root.mainloop()

In the example above, we create two Label widgets and use the place manager to manually specify their positions. The first label is placed at coordinates (50, 50), while the second label is placed at (100, 100).

Conclusion

In this blog post, we explored three commonly used geometry managers (pack, grid, place) in tkinter. Each manager offers its own advantages and disadvantages, so it’s important to choose the one that best suits your layout requirements and coding style. Having a solid understanding of these geometry managers will enable you to create more flexible and visually appealing GUI applications using tkinter in Python.