2017-02-13 16 views
0

클릭 한 번 키를 바인딩 해제/비활성화하려고하고 2 초 후에 기능을 재개합니다. 그러나 나는 언 바인딩을위한 코드를 이해할 수 없다. 바인드가 창에 있습니다. 지금까지 시도한 코드는 다음과 같습니다.파이썬 언 바운드/비활성화 후 키 바인딩을 클릭하고 나중에 다시 시작

self.choiceA = self.master.bind('a', self.run1) #bind key "a" to run1 
def run1(self, event=None): 
    self.draw_confirmation_button1() 
    self.master.unbind('a', self.choiceA) #try1: use "unbind", doesn't work 

    self.choiceA.configure(state='disabled') #try2: use state='disabled', doesn't't work, I assume it only works for button 
    self.master.after(2000, lambda:self.choiceA.configure(state="normal")) 

또한 2 초 후에 어떻게 키를 다시 사용할 수 있습니까?

정말 고마워요!

답변

0

self.master.unbind('a', self.choiceA) 두 번째 인수는 바인딩을 만들 때 반환 된 ID 대신 바인딩 해제 할 콜백이므로 작동하지 않습니다.

재 바인딩을 지연하려면 delay이 ms이고 callback이 인수를 사용하지 않는 함수 인 .after(delay, callback) 메서드를 사용해야합니다.

import tkinter as tk 

def callback(event): 
    print("Disable binding for 2s") 
    root.unbind("<a>", bind_id) 
    root.after(2000, rebind) # wait for 2000 ms and rebind key a 

def rebind(): 
    global bind_id 
    bind_id = root.bind("<a>", callback) 
    print("Bindind on") 


root = tk.Tk() 
# store the binding id to be able to unbind it 
bind_id = root.bind("<a>", callback) 

root.mainloop() 

비고 : 당신이 클래스를 사용하기 때문에, 내 bind_id 전역 변수는 (self.bind_id)에 대한 속성이 될 것입니다.