1 python events tkinter cursor
我正在构建一个代码,当用户将条目小部件的焦点从Entry小部件更改为任何位置时,我希望能够生成一个事件,例如另一个条目小部件,一个按钮......
到目前为止,我只想出了绑定到TAB和鼠标单击的想法,虽然如果我将鼠标单击绑定到Entry小部件,我只会在Entry小部件内部获得鼠标事件.
如何在窗口小部件失去光标焦点时生成事件?
任何帮助都感激不尽!
提前致谢!
威廉.
事件<FocusIn>和<FocusOut>是您想要的.运行以下示例,当您将焦点放在其中一个条目小部件中时,无论是单击还是按Tab键(或shift-tab),您都会看到焦点进出绑定.
from Tkinter import *
def main():
global text
root=Tk()
l1=Label(root,text="Field 1:")
l2=Label(root,text="Field 2:")
t1=Text(root,height=4,width=40)
e1=Entry(root)
e2=Entry(root)
l1.grid(row=0,column=0,sticky="e")
e1.grid(row=0,column=1,sticky="ew")
l2.grid(row=1,column=0,sticky="e")
e2.grid(row=1,column=1,sticky="ew")
t1.grid(row=2,column=0,columnspan=2,sticky="nw")
root.grid_columnconfigure(1,weight=1)
root.grid_rowconfigure(2,weight=1)
root.bind_class("Entry","<FocusOut>",focusOutHandler)
root.bind_class("Entry","<FocusIn>",focusInHandler)
text = t1
root.mainloop()
def focusInHandler(event):
text.insert("end","FocusIn %s\n" % event.widget)
text.see("end")
def focusOutHandler(event):
text.insert("end","FocusOut %s\n" % event.widget)
text.see("end")
if __name__ == "__main__":
main();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3102 次 |
| 最近记录: |