[英]How to make pop-up window with force attention in Tkinter
I want to create a window which doesn't allow the user to access other windows until you give an input.我想创建一个 window ,它不允许用户访问其他 windows ,直到您提供输入。 I tried
win.attribute("ontop", True)
but it allows the user to access other windows.我试过
win.attribute("ontop", True)
但它允许用户访问其他 windows。 or is there any function like a force_focus_lock()
in Tkinter python 3.8 which doesn't allow other window to get focus until you give a input or the close present window. or is there any function like a
force_focus_lock()
in Tkinter python 3.8 which doesn't allow other window to get focus until you give a input or the close present window.
I believe the below is what you are trying to do.我相信以下是您正在尝试做的事情。 Explanation is given in comments.
解释在评论中给出。
method #1: (PopOut1)方法 #1: (PopOut1)
method #2: (PopOut2)方法 #2: (PopOut2)
import tkinter as tk
#first method
class PopOut1(tk.Toplevel):
def __init__(self, master, **kwargs):
tk.Toplevel.__init__(self, master, **kwargs)
self.geometry('400x300')
#set focus to this window
self.focus_set()
#releasing on any other tkinter window, within this process, forces focus back to this window
self.grab_set()
#second method
class PopOut2(tk.Toplevel):
def __init__(self, master, **kwargs):
tk.Toplevel.__init__(self, master, **kwargs)
self.geometry('400x300')
#set focus to this window
self.focus_set()
#disable the main window
master.attributes('-disabled', True)
#so this window can't end up behind the disabled window
#only necessary if this window is not transient
#self.attributes('-topmost', True)
#capture close event
self.protocol("WM_DELETE_WINDOW", self.close)
#event=None ~ in case you also want to bind this to something
def close(self, event=None):
#re-enable the main window
self.master.attributes('-disabled', False)
#destroy this window
self.destroy()
class App(tk.Tk):
TITLE = 'Application'
WIDTH, HEIGHT, X, Y = 800, 600, 50, 50
def __init__(self):
tk.Tk.__init__(self)
tk.Button(self, text="open popout 1", command=self.open1).grid()
tk.Button(self, text="open popout 2", command=self.open2).grid()
def open1(self):
PopOut1(self)
def open2(self):
#.transient(self) ~
# flash PopOut if focus is attempted on main
# automatically drawn above parent
# will not appear in taskbar
PopOut2(self).transient(self)
if __name__ == '__main__':
app = App()
app.title(App.TITLE)
app.geometry(f'{App.WIDTH}x{App.HEIGHT}+{App.X}+{App.Y}')
#app.resizable(width=False, height=False)
app.mainloop()
Maybe creating 2 windows may do the job: one is where a user should enter something and another one full-screen transparent and always-on-top which will prevent the user from accessing another windows.也许创建 2 个 windows 可以完成这项工作:一个是用户应该输入内容的地方,另一个是全屏透明且始终在顶部的,这将阻止用户访问另一个 windows。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.