Jak*_*áha 3 python focus protocols tkinter
我有这个代码:
from tkinter import *
w = Tk()
w.protocol('WM_TAKE_FOCUS', print('hello world'))
mainloop()
Run Code Online (Sandbox Code Playgroud)
它只打印hello world
一次,然后停止工作.没有更多hello world
基本上WM_TAKE_FOCUS
不起作用.
您可以将函数绑定到<FocusIn>
事件.当您绑定到根窗口时,绑定将应用于根窗口中的每个窗口小部件,因此,如果您只想在窗口作为整体获得焦点时执行某些操作,则需要event.widget
与根窗口进行比较.
例如:
import Tkinter as tk
def handle_focus(event):
if event.widget == root:
print("I have gained the focus")
root = tk.Tk()
entry1 = tk.Entry(root)
entry2 = tk.Entry(root)
entry1.pack()
entry2.pack()
root.bind("<FocusIn>", handle_focus)
root.mainloop()
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
2189 次 |
最近记录: |