下面的代码中有两种方法,一种用于捕获鼠标点击,另一种用于按键。我想在按键上设置 focus_set 而不是鼠标点击,但是如果我在按键功能中使用 focus_set 则它不起作用。但是如果我把它放在鼠标里面,点击它就可以了,关键功能也很好用。
from tkinter import *
root = Tk()
text = ''
frame = Frame(root, width=100, height=100)
def key(event):
frame.focus_set() # here is the focus set which is not working
global text
text += event.char
print (test)
def callback(event):
#but If I put that same line here, it works
print ("clicked at", event.x, event.y)
frame.bind("<Key>", key)
frame.bind("<Button-1>", callback)
frame.pack()
root.mainloop()
focus_set
只影响调用后触发的事件,并且只影响键盘事件,不影响鼠标点击。在事件处理程序中调用它根本不会影响正在处理的事件。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句