Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to select at the same time from two Tkinter Listbox?
When building GUI applications with Tkinter, you may need to select items from multiple Listboxes simultaneously. By default, Tkinter's exportselection property causes selections to be cleared when you click on another widget. Setting exportselection=False maintains selections across multiple Listboxes.
Understanding exportselection Property
The exportselection property controls whether the selected items in a Listbox are exported to the system clipboard. When set to False, selections remain active even when interacting with other widgets.
Example
Here's how to create two Listboxes that maintain independent selections ?
# Import Tkinter library
from tkinter import *
# Create an instance of Tkinter frame or window
win = Tk()
# Set the geometry of tkinter frame
win.geometry("750x350")
win.title("Multiple Listbox Selection")
# Create ListBoxes with exportselection=False
listboxA = Listbox(win, exportselection=False)
listboxA.pack(padx=10, pady=10, fill=BOTH, expand=True)
listboxB = Listbox(win, exportselection=False)
listboxB.pack(padx=10, pady=10, fill=BOTH, expand=True)
# Add items to first listbox
listboxA.insert(1, "1.Python")
listboxA.insert(2, "2.Java")
listboxA.insert(3, "3.C++")
listboxA.insert(4, "4.Rust")
listboxA.insert(5, "5.GoLang")
# Add items to second listbox
listboxB.insert(1, "a.C#")
listboxB.insert(2, "b.JavaScript")
listboxB.insert(3, "c.R")
listboxB.insert(4, "d.PHP")
listboxB.insert(5, "e.CoffeeScript")
listboxB.insert(6, "f.Curl")
win.mainloop()
Getting Selected Items
You can retrieve selected items from both Listboxes using the curselection() method ?
from tkinter import *
def show_selections():
# Get selected indices from both listboxes
selection_a = listboxA.curselection()
selection_b = listboxB.curselection()
print("ListboxA selections:", [listboxA.get(i) for i in selection_a])
print("ListboxB selections:", [listboxB.get(i) for i in selection_b])
win = Tk()
win.geometry("750x400")
# Create listboxes with multiple selection mode
listboxA = Listbox(win, exportselection=False, selectmode=EXTENDED)
listboxA.pack(padx=10, pady=5, fill=BOTH, expand=True)
listboxB = Listbox(win, exportselection=False, selectmode=EXTENDED)
listboxB.pack(padx=10, pady=5, fill=BOTH, expand=True)
# Add items
languages_a = ["Python", "Java", "C++", "Rust", "GoLang"]
languages_b = ["C#", "JavaScript", "R", "PHP", "TypeScript"]
for lang in languages_a:
listboxA.insert(END, lang)
for lang in languages_b:
listboxB.insert(END, lang)
# Add button to show selections
Button(win, text="Show Selections", command=show_selections).pack(pady=10)
win.mainloop()
Key Properties
| Property | Default Value | Purpose |
|---|---|---|
exportselection |
True | Controls selection export to clipboard |
selectmode |
BROWSE | Defines selection behavior (SINGLE, BROWSE, MULTIPLE, EXTENDED) |
Conclusion
Set exportselection=False in Tkinter Listboxes to maintain selections when switching between widgets. Use selectmode=EXTENDED for multiple selections within each Listbox.
