在下面的代碼中,我將鍵綁定到文本小部件。根據綁定完成的方式,當在窗口小部件中鍵入「x」時,我會得到不同的結果。不同的方式綁定不同的結果?
爲什麼bind和bind_all之間的輸出有差異? 我應該如何使用所有三種變體,以便它們都能得到相同的結果?
import tkinter as tk
class Q_bind(tk.Text):
def __init__(self):
tk.Text.__init__(self)
self.bind("<Key>", self._insert_a) #-> 'a\nx' with break a!
#self.bind_class("<Key>", self._insert_a) # -> 'x' Replace with:
#self.bind_class("Text", "<Key>", self._insert_a) # -> 'a'!!
#self.bind_all("<Key>", self._insert_a) # -> 'xa\n'
print(self.bindtags()) #shows the bind-tags
def _insert_a(self, event=None):
print(event.char)
self.insert('end' ,"a\n")
return 'break' #added
if __name__ == "__main__":
app = Q_bind()
app.pack(fill="both", expand='y')
app.master.geometry('400x400')
app.mainloop()
不同的綁定用於不同的目的,它們以不同的方式工作(它們可以在'mainloop'檢查綁定時在不同的時刻執行)。 http://effbot.org/tkinterbook/tkinter-events-and-bindings.htm – furas
btw:它應該是'self.bind_class(「Text」,「」,self._insert_a)' - 類名稱爲「Text」 –
furas
這是否回答你的問題? http://stackoverflow.com/a/11542200/7432 –