rowspan/columnspan: Number of rows/columns to span
sticky: Alignment within the cell (N, E, S, W, or combinations)
padx/pady: External padding
ipadx/ipady: Internal padding
Grid Layout Preview
Username:
Password:
Grid with column/row span
Tip: Use grid_columnconfigure() and grid_rowconfigure() to make rows and columns expandable.
4. Creating Menus in Tkinter
Tkinter provides the Menu widget to create menu bars, pull-down menus, and popup menus.
Menu Example
import tkinter as tk
from tkinter import messagebox
def on_new():
print("New File")
def on_about():
messagebox.showinfo("About", "Tkinter Menu Example\nVersion 1.0")
root = tk.Tk()
root.title("Menu Example")
# Create a menu bar
menubar = tk.Menu(root)
root.config(menu=menubar)
# File menu
file_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=on_new, accelerator="Ctrl+N")
file_menu.add_command(label="Open...")
file_menu.add_separator()
file_menu.add_command(label="Exit", command=root.quit)
# Edit menu
edit_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut")
edit_menu.add_command(label="Copy")
edit_menu.add_command(label="Paste")
# Help menu
help_menu = tk.Menu(menubar, tearoff=0)
menubar.add_cascade(label="Help", menu=help_menu)
help_menu.add_command(label="About", command=on_about)
# Context menu
context_menu = tk.Menu(root, tearoff=0)
context_menu.add_command(label="Copy")
context_menu.add_command(label="Paste")
def show_context_menu(event):
context_menu.post(event.x_root, event.y_root)
# Bind right-click to show context menu
root.bind("", show_context_menu)
root.mainloop()
File
New
Open...
Exit
Edit
Cut
Copy
Paste
Help
About
Right-click anywhere to see context menu
Menu Types
Menu Bar: Top-level menu (File, Edit, etc.)
Pull-down Menu: Appears when clicking a menu item
Context Menu: Appears on right-click
Tear-off Menu: Can be detached (disabled by default in modern apps)
5. Dialog and Message Boxes
Tkinter provides several standard dialog boxes through the tkinter.messagebox and tkinter.filedialog modules.
Message Boxes
from tkinter import messagebox
# Show info message
messagebox.showinfo("Information", "This is an info message")
# Show warning
messagebox.showwarning("Warning", "This is a warning message")
# Show error
messagebox.showerror("Error", "This is an error message")
# Ask question (returns 'yes' or 'no')
response = messagebox.askquestion("Confirm", "Are you sure?")
if response == 'yes':
print("User clicked Yes")
# Ask yes/no/cancel (returns True/False/None)
response = messagebox.askyesnocancel("Save Changes", "Do you want to save changes?")
if response is True:
print("Save changes")
elif response is False:
print("Don't save")
else:
print("Cancel")
Information
This is an info message
File Dialogs
from tkinter import filedialog
from tkinter import messagebox
def open_file():
file_path = filedialog.askopenfilename(
title="Open File",
filetypes=(
("Text files", "*.txt"),
("All files", "*.*")
)
)
if file_path:
messagebox.showinfo("File Selected", f"Selected: {file_path}")
def save_file():
file_path = filedialog.asksaveasfilename(
defaultextension=".txt",
filetypes=(
("Text files", "*.txt"),
("All files", "*.*")
)
)
if file_path:
messagebox.showinfo("Save As", f"Will save to: {file_path}")
# Example usage in a Tkinter app
root = tk.Tk()
tk.Button(root, text="Open File", command=open_file).pack(pady=10)
tk.Button(root, text="Save As", command=save_file).pack(pady=10)
root.mainloop()
Color Chooser
from tkinter import colorchooser
def choose_color():
color = colorchooser.askcolor(title="Choose a color")
if color[1]: # If a color was selected
print(f"Selected color: {color[1]}")
# color[0] contains RGB tuple, color[1] contains hex string
# Example usage
root = tk.Tk()
tk.Button(root, text="Choose Color", command=choose_color).pack(pady=20)
root.mainloop()
6. Practice Exercise
Text Editor Application
Create a simple text editor with the following features:
A menu bar with File and Edit menus
File menu should have: New, Open, Save, Save As, and Exit
Edit menu should have: Cut, Copy, Paste, and Select All