[파이썬] tkinter 라벨(`Label`) 위젯

In this blog post, we will explore the tkinter Label widget in Python. The Label widget is used to display text or images on the screen in a graphical user interface (GUI) developed using the tkinter library.

Getting Started

To use the Label widget, we first need to import the tkinter module:

import tkinter as tk

Next, we create an instance of the Tk class to create the main window of our application:

window = tk.Tk()

Creating a Label

To create a Label widget, we use the following syntax:

label = tk.Label(window, text="Hello, World!")

In the above code, window is the parent widget to which the label belongs, and text is the text that will be displayed by the label.

Configuring the Label

We can configure various properties of the Label widget to customize its appearance. Some of the common properties include:

label.config(font=("Arial", 14, "bold"), fg="red", bg="white", width=20, height=3, anchor="center")

Placing the Label

Once the Label widget is configured, we need to place it within the window using a layout manager. The most commonly used layout manager is the pack() method, which automatically positions the widget based on available space.

label.pack()

Running the Application

To display the window and start the tkinter event loop, we call the mainloop() method:

window.mainloop()

Conclusion

In this blog post, we learned how to use the Label widget in tkinter to display text in a GUI application. We explored various ways to configure the label’s appearance and how to place it within the window. With this knowledge, you can now create visually appealing labels in your own tkinter applications.