Python Tkinter Keybinds和Buttons互相阻塞?

时间:2017-05-02 20:57:23

标签: python user-interface button tkinter bind

我有一段类似的代码:

def create_main(self):

    self.bind("<Left>", lambda e:self.function())
    self.button1 = Button(self, ...)
    self.button1.grid(row=0, column =0)

    #furtherbuttons...

def function(self):
    print('test')

我在这段代码中遇到的问题是,如果我向左按下,函数永远不会被调用。我尝试了不同的按钮和鼠标,鼠标按钮总是很好,但键盘什么也没做。

我读了一些关于阻止绑定操作的按钮,但没有解决这个问题。

1 个答案:

答案 0 :(得分:0)

这是一个在Win10上使用3.6.1的MCVE。

import tkinter as tk
root = tk.Tk()

def handle(event=None):
    print(event)
    return 'break'

root.bind('<Left>', handle)
tk.Button(root, text='button', command=handle)
root.mainloop()

两个&lt; - 并左键单击打印事件arg。没有干扰或阻塞。