这是一个针对Windows的解决方案,它使用
user32
dll中的
FlashWindowEx
。您需要向其传递一个
FLASHWINFO
对象。
grab_set
确保弹出窗口保持焦点并禁用主窗口中的任何小部件,使弹出窗口变为瞬态,确保它始终位于主控之上。
<Button-1>
事件用于检查鼠标单击,
winfo_containing
检查是否点击了除弹出窗口以外的其他窗口。然后将焦点设置回弹出窗口并闪烁处于焦点的窗口(这总是弹出窗口)。
您需要
pywin32才能使用此功能。
import Tkinter as tk
from ctypes import *
import win32con
class popup(object):
def __init__(self, parent):
self.parent = parent
self.root=tk.Toplevel(self.parent)
self.root.title("Popup")
self.root.grab_set()
self.root.transient(self.parent)
self.root.bind("<Button-1>", self.flash)
def flash(self, event):
if self.root.winfo_containing(event.x_root, event.y_root)!=self.root:
self.root.focus_set()
number_of_flashes = 5
flash_time = 80
info = FLASHWINFO(0,
windll.user32.GetForegroundWindow(),
win32con.FLASHW_ALL,
number_of_flashes,
flash_time)
info.cbSize = sizeof(info)
windll.user32.FlashWindowEx(byref(info))
class FLASHWINFO(Structure):
_fields_ = [('cbSize', c_uint),
('hwnd', c_uint),
('dwFlags', c_uint),
('uCount', c_uint),
('dwTimeout', c_uint)]
main = tk.Tk()
main.title("Main")
pop = popup(main)
main.mainloop()
现在,只有在单击主窗口的正文时才会出现闪烁,因此单击标题栏只会将焦点返回到弹出窗口而不会闪烁。要使其在这种情况下也触发闪烁,您可以尝试使用
<FocusOut>
事件,但您必须确保它仅在焦点转移到主窗口时才会发生,但实际上从未发生,因为使用了
grab_set
。您可能需要解决这个问题,但目前它运行得相当不错。因此它并不完美,但我希望它能有所帮助。