Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select at the same time from two Listbox?

Tags:

from Tkinter import *   master = Tk()  listbox = Listbox(master) listbox.pack() listbox.insert(END, "a list entry")  for item in ["one", "two", "three", "four"]:     listbox.insert(END, item)  listbox2 = Listbox(master) listbox2.pack() listbox2.insert(END, "a list entry")  for item in ["one", "two", "three", "four"]:     listbox2.insert(END, item)  master.mainloop() 

The code above creates a tkinter window with two listboxes. But there's a problem if you want to retrieve the values from both because, as soon as you select a value in one, it deselects whatever you selected in the other.

Is this just a limitation developers have to live with?

like image 902
directedition Avatar asked Apr 16 '09 15:04

directedition


People also ask

How to select multiple items in Tkinter listbox?

In Tkinter, multiple selections can be done using the List box widget. Generally, a Listbox displays different items in the form of a list. A list box widget provides one or more selections of items from a list. There are many options available in a Listbox widget that makes the user select multiple options.

How many items does a listbox hold by default?

The default is 20. If you want to allow the user to scroll the listbox horizontally, you can link your listbox widget to a horizontal scrollbar. Set this option to the . set method of the scrollbar.

What is a listbox in tkinter?

Introduction to the Tkinter Listbox A Listbox allows you to browse through the items and select one or multiple items at once. To create a listbox, you use the tk.Listbox class like this: listbox = tk.Listbox(container, listvariable, height) In this syntax: The container is the parent component of the listbox.


1 Answers

Short answer: set the value of the exportselection attribute of all listbox widgets to False or zero.

From a pythonware overview of the listbox widget:

By default, the selection is exported to the X selection mechanism. If you have more than one listbox on the screen, this really messes things up for the poor user. If he selects something in one listbox, and then selects something in another, the original selection is cleared. It is usually a good idea to disable this mechanism in such cases. In the following example, three listboxes are used in the same dialog:

b1 = Listbox(exportselection=0) for item in families:     b1.insert(END, item)  b2 = Listbox(exportselection=0) for item in fonts:     b2.insert(END, item)  b3 = Listbox(exportselection=0) for item in styles:     b3.insert(END, item) 

The definitive documentation for tk widgets is based on the Tcl language rather than python, but it is easy to translate to python. The exportselection attribute can be found on the standard options manual page.

like image 172
Jason Coon Avatar answered Sep 18 '22 14:09

Jason Coon