python 如何同时从两个Listbox中选择?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/756662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to select at the same time from two Listbox?
提问by directedition
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.
上面的代码创建了一个tkinter
带有两个列表框的窗口。但是,如果您想从两者中检索值,就会出现问题,因为一旦您在其中一个值中选择了一个值,它就会取消选择您在另一个中选择的任何值。
Is this just a limitation developers have to live with?
这只是开发人员必须忍受的限制吗?
回答by Jason Coon
Short answer: set the value of the exportselection
attribute of all listbox widgets to False or zero.
简短回答:将exportselection
所有列表框小部件的属性值设置为 False 或零。
From a pythonware overviewof the listbox widget:
从列表框小部件的pythonware概述:
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)
默认情况下,选择导出到 X 选择机制。如果屏幕上有多个列表框,这对可怜的用户来说真的是一团糟。如果他在一个列表框中选择了某些内容,然后在另一个列表框中选择了某些内容,则原始选择将被清除。在这种情况下禁用此机制通常是个好主意。在以下示例中,在同一个对话框中使用了三个列表框:
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.
tk 小部件的权威文档基于 Tcl 语言而不是 python,但很容易转换为 python。该exportselection
属性可以在标准选项手册页上找到。
回答by directedition
exportselection=0
when defining a listbox seems to take care of this issue.
exportselection=0
定义列表框时似乎解决了这个问题。