使用&lt; <listboxselect>&gt;使用多个列表框

时间:2017-08-18 20:36:36

标签: python python-3.x tkinter pycharm

我试图使用多个列表框作为显示信息树的方法。

# dictionary: {str : [str]}
dict_ = {some_class.get_dictionary()}
dict_keys = list[dict.keys()]
dict_keys.sort()

def selected_item(val):
    list_box_2.delete(0, END)

    sender = val.widget
    index = sender.curselection()
    value = sender.get(index)

    for thing in dict[value]:
        list_box_2.insert(END, thing)

list_box_1 = Listbox(display_frame, selectmode=SINGLE)
for item in dict_keys:
    list_box_1.insert(END, item)
list_box_1.bind("<<ListboxSelect>>", selected_item)
list_box_1.grid(sticky=W+E+N+S)

def display_selected(val):
    sender = val.widget
    index = sender.curselection()
    value = sender.get(index)
    # here I actually call methods from value, value is a class.
    # this in turn should populate a third list_box but I can't get there.
    print(value)

list_box_2 = Listbox(display_frame, selectmode=SINGLE)
list_box_2.bind("<<ListboxSelect>>", display_selected)
list_box_2.grid(column=0, row=1, sticky=W+E)

List_box_1按预期工作:它显示字典键,当用户点击其中一个项目时,它会使用所选键的字典内容填充list_box_2。但是,当我点击list_box_2中的项目时,它会调用selected_item()而不是display_selected(或者可能会调用两者,很难说),但这会导致list_box_2中的所有内容被删除(因为list_box_2.delete(0, END))。我认为这是因为绑定到"<<ListboxSelect>>",但我不确定。

有没有办法实现我正在寻找的功能?我环顾四周,绞尽脑汁,但我无法找到有关此事的任何信息。

由于

1 个答案:

答案 0 :(得分:1)

您应始终在Tkinter列表框中包含exportselection=0选项,尤其是当您同时在屏幕上显示多个选项时。没有此选项的默认设置是一种奇怪的模式,其中列表选择与系统剪贴板相关联;多个列表中的同时选择根本不存在(并且您将用户可能放入剪贴板中的任何内容丢弃)。

相关问题