📊 Unit-IV: Python GUI Programming with Tkinter

Lecture 2: Advanced Tkinter Widgets

1. The Scrollbar Widget

The Scrollbar widget is used to add scrolling capability to various widgets like Listbox, Text, and Canvas.

import tkinter as tk root = tk.Tk() root.title("Scrollbar Example") root.geometry("300x200") # Create a frame to hold the listbox and scrollbar frame = tk.Frame(root) frame.pack(fill=tk.BOTH, expand=True, padx=10, pady=10) # Create a scrollbar scrollbar = tk.Scrollbar(frame) scrollbar.pack(side=tk.RIGHT, fill=tk.Y) # Create a listbox listbox = tk.Listbox(frame, yscrollcommand=scrollbar.set) # Add items to the listbox for i in range(1, 51): listbox.insert(tk.END, f"Item {i}") listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True) # Configure the scrollbar to work with the listbox scrollbar.config(command=listbox.yview) root.mainloop()

Scrollbar with Listbox Example:

Item 1
Item 2
Item 3
Item 4
Item 5
Item 6
Item 7
Item 8
Item 9
Item 10

Scroll to see more items...

2. The Listbox Widget

The Listbox widget is used to display a list of items from which a user can select one or more items.

def on_select(event): selected_indices = listbox.curselection() selected_items = [listbox.get(i) for i in selected_indices] print("Selected items:", selected_items) # Create a listbox with multiple selection enabled listbox = tk.Listbox( root, selectmode=tk.MULTIPLE, # or tk.SINGLE for single selection height=5 ) # Add items to the listbox items = ["Apple", "Banana", "Orange", "Mango", "Grapes"] for item in items: listbox.insert(tk.END, item) # Bind the selection event listbox.bind('<>', on_select) listbox.pack(pady=10)

Listbox Example (select multiple with Ctrl+Click):

Press Ctrl+Click to select multiple items

3. The Combobox Widget

The Combobox widget combines an Entry with a dropdown list. It's part of the ttk module.

from tkinter import ttk def on_combobox_select(event): print(f"Selected: {combo.get()}") # Create a combobox combo = ttk.Combobox( root, values=["Option 1", "Option 2", "Option 3", "Option 4"], state="readonly" # Prevent typing in the combobox ) combo.set("Select an option") combo.pack(pady=10) # Bind the selection event combo.bind("<>", on_combobox_select)

Combobox Example:

4. The Spinbox Widget

The Spinbox widget allows the user to select from a fixed set of values or a range of numbers.

def on_spinbox_change(): print(f"Current value: {spinbox.get()}") # Create a spinbox with a range of values spinbox = tk.Spinbox( root, from_=0, to=100, increment=5, command=on_spinbox_change, width=10 ) spinbox.pack(pady=10) # You can also specify custom values spinbox2 = tk.Spinbox( root, values=("Jan", "Feb", "Mar", "Apr", "May"), width=10 ) spinbox2.pack(pady=5)

Spinbox Examples:

5. The Checkbutton Widget

The Checkbutton widget is used to display a number of options as checkboxes.

def on_checkbutton_toggle(): selected = [options[i] for i, var in enumerate(check_vars) if var.get() == 1] print("Selected options:", selected) options = ["Option 1", "Option 2", "Option 3"] check_vars = [] # Create a frame to hold the checkbuttons frame = tk.Frame(root) frame.pack(pady=10) # Create checkbuttons for i, option in enumerate(options): var = tk.IntVar() check_vars.append(var) cb = tk.Checkbutton( frame, text=option, variable=var, command=on_checkbutton_toggle ) cb.pack(anchor='w')

Checkbutton Example:

6. The Radiobutton Widget

The Radiobutton widget is used to implement one-of-many selections.

def on_radiobutton_select(): print(f"Selected option: {radio_var.get()}") # Variable to hold the selected radio button value radio_var = tk.StringVar() radio_var.set("Option 1") # Set default selection # Create a frame to hold the radiobuttons frame = tk.Frame(root) frame.pack(pady=10) # Create radiobuttons options = ["Option 1", "Option 2", "Option 3"] for option in options: rb = tk.Radiobutton( frame, text=option, variable=radio_var, value=option, command=on_radiobutton_select ) rb.pack(anchor='w')

Radiobutton Example:

Summary

In this lecture, we've covered several important Tkinter widgets:

  • Scrollbar: For adding scrolling to other widgets
  • Listbox: For displaying a list of selectable items
  • Combobox: A dropdown list with an editable field
  • Spinbox: For selecting from a range of values
  • Checkbutton: For multiple selections
  • Radiobutton: For single selection from multiple options

Practice Exercise:

Create a Tkinter application that includes:

  1. A Listbox with multiple selection enabled, populated with at least 10 items
  2. A Scrollbar connected to the Listbox
  3. A Combobox with a list of options
  4. A group of Checkbuttons for multiple selections
  5. A group of Radiobuttons for single selection
  6. A button that, when clicked, displays all selected options from all widgets