Introduction

Basic Widgets

Toplevel Widgets

Geometry Management

Binding Functions

Working with Images in Tkinter

Tkinter Advance

Applications and Projects

Creating a multiple Selection using Tkinter

If you want to allow users to make multiple selections in Tkinter, the Listbox widget with the MULTIPLE selection mode is what you need.

Here's a tutorial on how to create a multiple selection interface using Tkinter:

Multiple Selection using Listbox in Tkinter

  1. Setting up the basic GUI with Tkinter:

    Initialize a basic window.

    import tkinter as tk
    
    root = tk.Tk()
    root.title("Multiple Selection with Tkinter")
    root.geometry("300x300")
    
  2. Creating and Configuring the Listbox:

    Use the Listbox widget with selectmode=tk.MULTIPLE for multiple selections.

    # Create the listbox and populate it with options
    listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
    options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
    for option in options:
        listbox.insert(tk.END, option)
    listbox.pack(pady=20)
    
  3. Function to Retrieve Selected Items:

    def get_selections():
        selected_indices = listbox.curselection()
        selected_items = [listbox.get(i) for i in selected_indices]
        print("Selected Items:", selected_items)
    

    The curselection method returns a tuple of indices of the selected items. We then retrieve the selected items using these indices.

  4. Add Button to Display Selections:

    btn = tk.Button(root, text="Get Selections", command=get_selections)
    btn.pack(pady=20)
    
  5. Run the main loop:

    root.mainloop()
    

Now, put all the code snippets together and run the script. You'll see a Listbox with multiple options. You can select multiple items by holding down the Ctrl key (Cmd key on macOS) and clicking on the desired options. Click the "Get Selections" button to print the selected items to the console.

This approach allows you to create a basic multiple selection interface. You can extend this functionality by adding more features or integrating with other parts of a larger Tkinter application.

  1. Python Tkinter checkbox selection example:

    • Description: Use Tkinter Checkbutton to create a checkbox for user selection.
    • Code Example:
      import tkinter as tk
      
      def on_checkbox_click():
          selected_value.set("Checkbox is " + ("checked" if checkbox_var.get() else "unchecked"))
      
      root = tk.Tk()
      root.title("Tkinter Checkbox Selection Example")
      
      # Create a checkbox
      checkbox_var = tk.BooleanVar()
      checkbox = tk.Checkbutton(root, text="Check me", variable=checkbox_var, command=on_checkbox_click)
      checkbox.pack(pady=10)
      
      selected_value = tk.StringVar()
      label = tk.Label(root, textvariable=selected_value)
      label.pack(pady=10)
      
      root.mainloop()
      
  2. Tkinter listbox multiple selection:

    • Description: Enable multiple selection in a Tkinter Listbox widget.
    • Code Example:
      import tkinter as tk
      
      def on_selection(event):
          selected_items = listbox.curselection()
          selected_values.set([listbox.get(index) for index in selected_items])
      
      root = tk.Tk()
      root.title("Tkinter Listbox Multiple Selection")
      
      # Create a Listbox with multiple selection
      listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
      listbox.pack(pady=10)
      
      # Add items to the Listbox
      items = ["Item 1", "Item 2", "Item 3", "Item 4", "Item 5"]
      for item in items:
          listbox.insert(tk.END, item)
      
      # Bind selection event
      listbox.bind("<<ListboxSelect>>", on_selection)
      
      selected_values = tk.StringVar()
      label = tk.Label(root, textvariable=selected_values)
      label.pack(pady=10)
      
      root.mainloop()
      
  3. How to create a multiselect dropdown in Tkinter:

    • Description: Implement a multiselect dropdown in Tkinter using the Listbox widget with multiple selection.
    • Code Example:
      import tkinter as tk
      
      def on_dropdown_selection():
          selected_items = listbox.curselection()
          selected_values.set([listbox.get(index) for index in selected_items])
      
      root = tk.Tk()
      root.title("Tkinter Multiselect Dropdown")
      
      # Create a dropdown with multiple selection
      listbox = tk.Listbox(root, selectmode=tk.MULTIPLE)
      listbox.pack(pady=10)
      
      # Add items to the dropdown
      items = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
      for item in items:
          listbox.insert(tk.END, item)
      
      # Bind selection event
      listbox.bind("<<ListboxSelect>>", lambda event: on_dropdown_selection())
      
      selected_values = tk.StringVar()
      label = tk.Label(root, textvariable=selected_values)
      label.pack(pady=10)
      
      root.mainloop()
      
  4. Tkinter multiple choice options:

    • Description: Provide multiple choice options using Tkinter Checkbutton widgets.
    • Code Example:
      import tkinter as tk
      
      def on_checkbox_click(checkbox_var, label_text):
          selected_value.set("Selected: " + label_text if checkbox_var.get() else "")
      
      root = tk.Tk()
      root.title("Tkinter Multiple Choice Options")
      
      # Create multiple choice options using Checkbuttons
      options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
      selected_value = tk.StringVar()
      
      for option in options:
          checkbox_var = tk.BooleanVar()
          checkbox = tk.Checkbutton(root, text=option, variable=checkbox_var,
                                    command=lambda var=checkbox_var, text=option: on_checkbox_click(var, text))
          checkbox.pack()
      
      label = tk.Label(root, textvariable=selected_value)
      label.pack(pady=10)
      
      root.mainloop()
      
  5. Checkbutton group in Tkinter:

    • Description: Group multiple Checkbutton widgets in Tkinter using a shared variable.
    • Code Example:
      import tkinter as tk
      
      def on_checkbox_click(checkbox_var, label_text):
          selected_value.set("Selected: " + label_text if checkbox_var.get() else "")
      
      root = tk.Tk()
      root.title("Tkinter Checkbutton Group")
      
      # Group Checkbuttons using a shared variable
      checkbox_var = tk.BooleanVar()
      options = ["Option 1", "Option 2", "Option 3", "Option 4", "Option 5"]
      selected_value = tk.StringVar()
      
      for option in options:
          checkbox = tk.Checkbutton(root, text=option, variable=checkbox_var,
                                    command=lambda text=option: on_checkbox_click(checkbox_var, text))
          checkbox.pack()
      
      label = tk.Label(root, textvariable=selected_value)
      label.pack(pady=10)
      
      root.mainloop()