如何從兩個Listbox中同時選擇?

2024-04-25 22:21:41 发布

您现在位置:Python中文网/ 问答频道 /正文

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()

上面的代码创建了一个带有两个列表框的tkinter窗口。但是如果您想从这两个值中检索值,则会出现问题,因为一旦您在其中一个值中选择了一个值,它就会取消选择您在另一个值中选择的任何值。

这仅仅是开发人员必须面对的限制吗?


Tags: inmasterforitemonepacklistend
2条回答

简而言之:将所有listbox小部件的exportselection属性的值设置为False或零。

从列表框小部件的a pythonware overview

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)

tk小部件的最终文档是基于Tcl语言而不是python的,但是很容易翻译成python。可以在standard options manual page上找到exportselection属性。

exportselection=0在定义列表框时,似乎可以解决此问题。

相关问题 更多 >