打破tkinter列表框

时间:2015-04-07 17:12:18

标签: python tkinter escaping break

我正在尝试将escape绑定到tkinter列表框,当我点击escape时,它仍然想要运行apply函数。

def body(self, master):
        self.e1 = tk.Listbox(master, selectmode=tk.SINGLE, height = 10, exportselection=0)

        for name in self.names:
            self.e1.insert(tk.END, str(name))


        self.selection = 0
        self.e1.select_set(self.selection)

        self.e1.bind("<Down>", self.OnEntryDown)
        self.e1.bind("<Up>", self.OnEntryUp)
        self.e1.bind("<Escape>", self.test)


        self.e1.pack()

        return self.e1

    def test(self, event):
        self.destroy()

    def OnEntryDown(self, event):
        #self.e1.yview_scroll(1,"units")
        if self.selection < self.e1.size()-1:
            self.e1.select_clear(self.selection)
            self.selection += 1
            self.e1.select_set(self.selection)

    def OnEntryUp(self, event): 
        if self.selection > 0:
            self.e1.select_clear(self.selection)
            self.selection -= 1
            self.e1.select_set(self.selection)

    def apply(self):
        self.file.returnSelection(self.e1.get(self.e1.curselection()))

我能否对我做错了什么有所了解?我没有收到错误,但它只是调用apply方法而不是开箱即用。

谢谢!

1 个答案:

答案 0 :(得分:0)

想出来。我在我试图操作的函数中放了一个if语句,如果操作的变量不是none,则继续。否则,它没有执行代码,我没有抛出任何错误