我已经尝试了一段时间,但是还没有找到一种方法来做到这一点。
我有一个tkinter脚本,当按下按钮时会创建一个弹出窗口。但是,我不希望用户能够从该窗口单击移至创建的任何先前窗口。我已经使用root.grab_set()进行了此操作,但是没有向用户指示他们必须留在该窗口中。
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
Run Code Online (Sandbox Code Playgroud)
例如,当您有一个由文件对话模块创建的窗口时,如果您尝试单击另一个窗口,则文件对话窗口将停留在顶部,并具有“闪烁的”动画,让用户知道他们无法单击。有什么方法可以重现这种效果吗?进行文件对话的源代码对我来说并不成功,Google搜索也没有。
我能想到的最简单的方法是使用事件和焦点命令以及窗口bell命令:
#!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()
Run Code Online (Sandbox Code Playgroud)