2017-05-02 49 views
0

我有一段類似的代碼如下:Python Tkinter Keybinds和按鈕彼此阻塞?

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

請添加更多,併發佈一個MCVE https://stackoverflow.com/help/mcve。 –

+0

按鈕不會阻止綁定操作。見http://stackoverflow.com/q/16923167/7432 –

+0

我假設「自我」是一個框架?您可能需要綁定到根。 – Novel

回答

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() 

兩個< - 和左擊打印事件arg。沒有干擾或阻塞。