Python > GUI Programming with Python > Tkinter > Common Widgets (Buttons, Labels, Entry Fields, Lists, etc.)
Tkinter Basic Widgets Example
This snippet demonstrates the usage of common Tkinter widgets such as Labels, Entry fields, and Buttons. It provides a basic GUI where users can enter text and click a button to display a greeting.
Complete Code
This code creates a simple GUI application with a Label, an Entry field, and a Button. When the user clicks the 'Greet' button, the application retrieves the name entered in the Entry field and displays a personalized greeting in a Label. The `ttk` module is used for themed widgets.
import tkinter as tk
from tkinter import ttk
class GreetingApp:
def __init__(self, root):
self.root = root
self.root.title("Greeting App")
# Label
self.name_label = ttk.Label(root, text="Enter your name:")
self.name_label.grid(row=0, column=0, padx=10, pady=5, sticky=tk.W)
# Entry Field
self.name_entry = ttk.Entry(root, width=30)
self.name_entry.grid(row=0, column=1, padx=10, pady=5)
# Button
self.greet_button = ttk.Button(root, text="Greet", command=self.greet)
self.greet_button.grid(row=1, column=0, columnspan=2, pady=10)
# Result Label
self.result_label = ttk.Label(root, text="")
self.result_label.grid(row=2, column=0, columnspan=2, pady=5)
def greet(self):
name = self.name_entry.get()
if name:
self.result_label.config(text=f"Hello, {name}!")
else:
self.result_label.config(text="Please enter your name.")
if __name__ == "__main__":
root = tk.Tk()
app = GreetingApp(root)
root.mainloop()
Concepts Behind the Snippet
This snippet demonstrates the basic structure of a Tkinter application. It covers the following concepts:
tk.Tk() creates the main application window.grid() method is used to arrange widgets in a grid layout within the window.command attribute of the Button is used to bind the greet function to the button's click event.config() method is used to update the properties of a widget, such as the text displayed in a Label.
Real-Life Use Case
This basic structure can be extended to create more complex GUI applications, such as:
Best Practices
ttk module provides widgets that are themed to match the operating system, providing a more native look and feel.grid, pack, or place) based on the complexity and requirements of the GUI. grid is often preferred for its flexibility.
When to Use Them
Tkinter widgets are appropriate for creating:
Alternatives
Other GUI libraries for Python include:
Pros
Cons
ttk module helps to improve this.
FAQ
-
How do I change the font size of a Label?
You can change the font size of a Label using thefontoption:my_label = ttk.Label(root, text="My Label", font=("Arial", 12))You can also specify the font style (e.g., "bold", "italic"). -
How do I change the color of a Button?
You can change the background and foreground colors of a Button using thebackgroundandforegroundoptions:my_button = ttk.Button(root, text="My Button", background="blue", foreground="white")Note thatttkwidgets might have different styling options; you might need to use styles for more advanced customization. -
How do I prevent the window from resizing?
You can disable resizing with `root.resizable(False, False)`. The first argument controls horizontal resizing, and the second controls vertical resizing.