Tkinter:尝试单击时窗口闪烁 [英] Tkinter: Window flash when attempting to click away
问题描述
我已经尝试这样做了一段时间,但还没有想出办法.
Ive been trying to do this for a while now, but haven't figured out a way to do it.
我有一个 tkinter 脚本,它在按下按钮时创建一个弹出窗口.但是我不希望用户能够从这个窗口点击到任何以前创建的窗口.我已经使用 root.grab_set() 进行了这项工作,但是没有迹象表明用户必须留在该窗口上.
I have a tkinter script, that creates a popup window when a button is pressed. However I don't want the user to be able to click away from this window to any previous windows created. I have got this working with root.grab_set(), however there is no indication to the user that they must stay on that window.
class popup(object):
def __init__(self, parent):
self.root=Toplevel(parent)
self.root.grab_set() #prevents the user clicking on the parent window
#But the window doesnt 'flash' when an attempt to click away is made
例如,当您有一个由文件对话模块创建的窗口时,如果您尝试点击另一个窗口,文件对话窗口会停留在顶部并有一个闪烁"动画,让用户知道他们不能点击离开.有没有办法重现这种效果?查阅文件对话的来源对我来说并没有什么成果,谷歌搜索也没有.
For example, when you have a window created by the filedialogue module, if you attempt to click onto another window the filedialogue window stays on top and has a 'flashing' animation to let the user know they cant click away. Is there a way I can reproduce this effect? Going through the source of filedialogue hasn't been fruitful for me, and neither have Google searches.
推荐答案
我能想到的最简单的方法是使用事件和焦点命令,以及 windows bell
命令:
The simplest way i can think to do this is to use an event and the focus commands, along with the windows bell
command:
#!python3
import tkinter as tk
class popup(object):
def __init__(self, parent):
self.root=tk.Toplevel(parent)
self.root.title("Popup")
self.root.bind("<FocusOut>", self.Alarm)
def Alarm(self, event):
self.root.focus_force()
self.root.bell()
main = tk.Tk()
main.title("Main")
pop = popup(main)
main.mainloop()
这篇关于Tkinter:尝试单击时窗口闪烁的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!