[파이썬] tkinter 프레임(`Frame`) 위젯

The tkinter library in Python provides a rich set of GUI (Graphical User Interface) components for building interactive applications. One of the most commonly used components is the Frame widget. A Frame widget acts as a container for other widgets, allowing you to organize and group them together.

Creating a Frame widget

To create a Frame widget in tkinter, you first need to import the library and create an instance of the Tk class, which represents the main window of your application. Then, you can create a Frame widget using the Frame class constructor. Here’s an example:

import tkinter as tk

# Create the main window
root = tk.Tk()

# Create a frame widget
frame = tk.Frame(root, width=200, height=200)
frame.pack()

# Run the main event loop
root.mainloop()

In the above example, we import the tkinter library and create an instance of the Tk class called root. Then, we create a Frame widget called frame and specify its dimensions using the width and height parameters. Finally, we use the pack method to make the frame visible within the main window, and the mainloop method to start the application’s event loop.

Adding widgets to a Frame

Once you have created a Frame widget, you can add other widgets to it by using their respective constructor functions (e.g., Button, Label, etc) and specifying the Frame widget as the parent. Here’s an example that adds a Label widget to the previously created frame:

import tkinter as tk

# Create the main window
root = tk.Tk()

# Create a frame widget
frame = tk.Frame(root, width=200, height=200)
frame.pack()

# Add a label widget to the frame
label = tk.Label(frame, text="Hello, tkinter!")
label.pack()

# Run the main event loop
root.mainloop()

In the above example, we create a Label widget called label and specify the frame as its parent. We also set the text property of the label to display the message “Hello, tkinter!”. Finally, we use the pack method to make the label visible within the frame.

Customizing a Frame

The Frame widget in tkinter provides various options to customize its appearance and behavior. Some common options include background (to set the background color), borderwidth (to set the width of the border), relief (to set the type of border), and more. Here’s an example that demonstrates some of these options:

import tkinter as tk

# Create the main window
root = tk.Tk()

# Create a frame widget
frame = tk.Frame(root, width=200, height=200, background="yellow", borderwidth=3, relief="groove")
frame.pack()

# Add a label widget to the frame
label = tk.Label(frame, text="Hello, tkinter!", background="white")
label.pack()

# Run the main event loop
root.mainloop()

In the above example, we set the background color of the frame to “yellow” using the background option. We also set the width of the frame’s border to 3 pixels and set the type of border to “groove” using the borderwidth and relief options, respectively. Additionally, we set the background color of the label widget to “white” using the background option.

With the Frame widget in tkinter, you can create organized and visually appealing user interfaces by grouping related widgets together. Experiment with different options and layouts to design interactive applications that meet your specific requirements.