Tkinter列表框功能错误

时间:2019-03-21 04:30:59

标签: python tkinter

在我的代码中,我必须列出绑定到2个不同功能的框。 这是代码:

from tkinter import *
import string

class App:

    def change_dropdown(self, *args):
        print(self.Lb1.curselection())
        self.Lb2.insert(self.count, self.choices[self.Lb1.curselection()[0]])
        self.count+=1

    def delete_dropdown_selected(self, *args):
        print(self.Lb2.curselection())


    def __init__(self, master):

        self.count = 0

        self.left = Frame(master)
        self.left.config()
        self.left.pack(side=LEFT)

        self.choices = []

        self.yscroll = Scrollbar(master, orient=VERTICAL)

        self.Lb1 = Listbox(self.left, selectmode=SINGLE, yscrollcommand=self.yscroll.set, font=50, bd=2)
        self.Lb2 = Listbox(self.left, selectmode=SINGLE, bd=2)

        for j in range(2):
            for i in range(26):
               self.Lb1.insert(i,string.ascii_lowercase[i])
               self.choices.append(string.ascii_letters[i])

        self.Lb1.config(width=50, height=30)
        self.Lb1.pack(side=TOP, fill=BOTH, expand=1)
        self.Lb2.config(font=30, width=50, height=10)
        self.Lb2.pack(side=BOTTOM, fill=BOTH, expand=1, pady=10)
        self.Lb2.bind('<<ListboxSelect>>', self.delete_dropdown_selected)
        self.Lb1.bind('<<ListboxSelect>>', self.change_dropdown)
        self.yscroll.pack(side=LEFT, fill=Y)
        self.yscroll.config(command=self.Lb1.yview)


root = Tk()
root.resizable(width=False, height=False)
app = App(root)
root.mainloop()

问题是,当我单击Lb2中的项目时,它会转到 change_dropdown(),而不是 delete_dropdown_selected()。我不明白为什么,因为我在这里指定了

self.Lb2.bind('<<ListboxSelect>>', self.delete_dropdown_selected)

1 个答案:

答案 0 :(得分:0)

在Tkinter列表框中使用exportselection=0选项。

self.Lb1 = Listbox(self.left, selectmode=SINGLE, yscrollcommand=self.yscroll.set, font=50, bd=2, exportselection=0)
self.Lb2 = Listbox(self.left, selectmode=SINGLE, bd=2, exportselection=0)

How to keep selections highlighted in a tkinter Listbox?