如何同时从两个列表框中选择? [英] How to select at the same time from two Listbox?

查看:113
本文介绍了如何同时从两个列表框中选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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窗口.但是,如果您想从这两个值中检索值,就会出现问题,因为一旦在一个值中选择了一个值,它就会取消选择在另一个值中选择的值.

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?

推荐答案

简短的答案:将所有列表框小部件的exportselection属性的值设置为False或零.

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

来自 a列表框小部件的pythonware概述:

默认情况下,所选内容已导出 X选择机制.如果你 上有多个列表框 屏幕,这真的搞砸了 对于贫穷的用户.如果他选择 一个列表框中的内容,然后 选择另一个 原始选择已清除.它是 通常禁用此功能是个好主意 在这种情况下的机制.在里面 在下面的示例中,三个列表框是 在同一对话框中使用:

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.可以在标准选项手册页上找到exportselection属性.

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.

这篇关于如何同时从两个列表框中选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆