Creating GUI (Graphical User Interface) applications in Python is straightforward with the Tkinter module, which is part of the Python standard library. Tkinter provides a set of tools to create windows, buttons, labels, and other UI components for desktop applications.
1. Basic Window Example
This simple example demonstrates how to create a basic window using Tkinter.
import tkinter as tk# Create the main windowwindow = tk.Tk()window.title("My First Tkinter Application")# Set the window sizewindow.geometry("400x300")# Run the windowwindow.mainloop()
Explanation:
tk.Tk(): Creates the main window.
window.title(): Sets the title of the window.
window.geometry(): Specifies the dimensions of the window.
window.mainloop(): Keeps the window open and waits for user actions.
2. Adding Labels and Buttons
This example shows how to add a label and a button to the Tkinter window. When the button is clicked, the label text changes.
Explanation:
Label: A widget to display text.
Button: A widget that triggers a function when clicked (using the command attribute).
3. Text Entry Box
This example adds a text entry box where users can type their name and a button that greets them.
Explanation:
Entry: A widget for single-line text input.
get(): Method used to retrieve the text entered in the entry widget.
4. Using Frames for Layout Management
In this example, we use a frame to organize widgets in a structured manner.
Explanation:
Frame: A container widget that allows you to organize other widgets inside it.
5. Adding Checkbuttons
This example demonstrates how to add checkboxes (Checkbuttons) to the Tkinter window.
Explanation:
Checkbutton: A widget used to display checkboxes that can be selected or deselected.
IntVar(): A special type of variable used with checkbuttons to store the state (checked or unchecked).
6. Radio Buttons for Single Choice
In this example, we use radio buttons to allow users to choose one option from a set.
Explanation:
Radiobutton: A widget that allows the user to select one option from a list.
StringVar(): A special type of variable used with radio buttons to store the selected value.
7. Canvas for Drawing Shapes
This example shows how to use a canvas to draw shapes and lines in a Tkinter window.
Explanation:
Canvas: A widget used for drawing shapes, lines, and images.
create_rectangle(): Draws a rectangle on the canvas.
create_oval(): Draws an oval on the canvas.
create_line(): Draws a line on the canvas.
8. Using Messagebox for Alerts
This example uses Tkinter's built-in messagebox module to show alert dialogs.
Explanation:
messagebox.showinfo(): Displays an informational alert box.
9. Grid Layout
This example demonstrates the use of grid layout to organize widgets in a table-like structure.
Explanation:
grid(): A layout manager that places widgets in a table-like structure. Widgets are arranged by specifying their row and column positions.
These are just a few examples of what you can do with Tkinter. You can create complex, interactive applications by combining different widgets and layout options, such as menus, dialogs, and event handling.
import tkinter as tk
# Function to update label text
def update_label():
label.config(text="Button Clicked!")
# Create the main window
window = tk.Tk()
window.title("Label and Button Example")
# Add a label to the window
label = tk.Label(window, text="Hello, Tkinter!", font=("Arial", 14))
label.pack(pady=20)
# Add a button that updates the label when clicked
button = tk.Button(window, text="Click Me", command=update_label)
button.pack(pady=20)
# Run the window
window.mainloop()
import tkinter as tk
# Function to greet the user
def greet_user():
name = entry.get()
greeting_label.config(text=f"Hello, {name}!")
# Create the main window
window = tk.Tk()
window.title("Text Entry Example")
# Add a label
label = tk.Label(window, text="Enter your name:", font=("Arial", 12))
label.pack(pady=10)
# Add a text entry box
entry = tk.Entry(window, font=("Arial", 12))
entry.pack(pady=10)
# Add a button to greet the user
button = tk.Button(window, text="Greet Me", command=greet_user)
button.pack(pady=10)
# Label to display the greeting
greeting_label = tk.Label(window, text="", font=("Arial", 14))
greeting_label.pack(pady=20)
# Run the window
window.mainloop()
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Frame Example")
# Create a frame to hold the widgets
frame = tk.Frame(window)
frame.pack(pady=20)
# Add a label inside the frame
label = tk.Label(frame, text="This is inside a frame", font=("Arial", 14))
label.pack()
# Add a button inside the frame
button = tk.Button(frame, text="Click Me", command=lambda: label.config(text="Button Clicked!"))
button.pack(pady=10)
# Run the window
window.mainloop()
import tkinter as tk
# Function to display selected options
def show_selection():
selection = "You selected: "
if var1.get() == 1:
selection += "Option 1 "
if var2.get() == 1:
selection += "Option 2 "
if var3.get() == 1:
selection += "Option 3"
label.config(text=selection)
# Create the main window
window = tk.Tk()
window.title("Checkbutton Example")
# Define variables for checkbuttons
var1 = tk.IntVar()
var2 = tk.IntVar()
var3 = tk.IntVar()
# Add checkbuttons
check1 = tk.Checkbutton(window, text="Option 1", variable=var1)
check1.pack()
check2 = tk.Checkbutton(window, text="Option 2", variable=var2)
check2.pack()
check3 = tk.Checkbutton(window, text="Option 3", variable=var3)
check3.pack()
# Add a button to show selected options
button = tk.Button(window, text="Show Selection", command=show_selection)
button.pack(pady=20)
# Add a label to display the selection
label = tk.Label(window, text="", font=("Arial", 12))
label.pack()
# Run the window
window.mainloop()
import tkinter as tk
# Function to display the selected option
def show_choice():
choice = var.get()
label.config(text=f"You selected: {choice}")
# Create the main window
window = tk.Tk()
window.title("Radio Button Example")
# Define the variable to store selected choice
var = tk.StringVar()
# Add radio buttons
radio1 = tk.Radiobutton(window, text="Option 1", variable=var, value="Option 1")
radio1.pack()
radio2 = tk.Radiobutton(window, text="Option 2", variable=var, value="Option 2")
radio2.pack()
radio3 = tk.Radiobutton(window, text="Option 3", variable=var, value="Option 3")
radio3.pack()
# Add a button to display the selected option
button = tk.Button(window, text="Show Choice", command=show_choice)
button.pack(pady=20)
# Add a label to display the result
label = tk.Label(window, text="", font=("Arial", 12))
label.pack()
# Run the window
window.mainloop()
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Canvas Example")
# Create a canvas widget
canvas = tk.Canvas(window, width=400, height=400, bg="white")
canvas.pack()
# Draw shapes on the canvas
canvas.create_rectangle(50, 50, 150, 150, outline="black", fill="blue")
canvas.create_oval(200, 200, 300, 300, outline="red", fill="green")
canvas.create_line(50, 200, 300, 50, fill="purple", width=2)
# Run the window
window.mainloop()
import tkinter as tk
from tkinter import messagebox
# Function to show an alert message
def show_alert():
messagebox.showinfo("Information", "This is an alert message!")
# Create the main window
window = tk.Tk()
window.title("Messagebox Example")
# Add a button to trigger the alert
button = tk.Button(window, text="Show Alert", command=show_alert)
button.pack(pady=20)
# Run the window
window.mainloop()
import tkinter as tk
# Create the main window
window = tk.Tk()
window.title("Grid Layout Example")
# Add labels and buttons in a grid layout
tk.Label(window, text="Name:").grid(row=0, column=0)
tk.Entry(window).grid(row=0, column=1)
tk.Label(window, text="Age:").grid(row=1, column=0)
tk.Entry(window).grid(row=1, column=1)
tk.Button(window, text="Submit").grid(row=2, column=0, columnspan=2)
# Run the window
window.mainloop()