如何解决如何在Tkinter中用力使弹出窗口
我想创建一个窗口,除非您提供输入,否则该窗口不允许用户访问其他窗口。
我尝试了win.attribute("ontop",True)
,但它允许用户访问其他窗口。
还是在Tkinter python 3.8中有像force_focus_lock()
这样的函数,不允许其他窗口执行
获得焦点,直到您提供输入或关闭当前窗口。
解决方法
我相信以下是您要尝试做的。注释中给出了解释。
方法1:(PopOut1)
- 您仍然可以移动主窗口
- 如果主窗口上有鼠标释放键,则新窗口将占据焦点
方法2:(PopOut2)
- 主窗口被锁定在适当的位置
- 如果在主窗口上释放鼠标,新窗口将假定焦点,闪烁和“叮”声
import tkinter as tk
#first method
class PopOut1(tk.Toplevel):
def __init__(self,master,**kwargs):
tk.Toplevel.__init__(self,**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,**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()
,
也许可以创建2个窗口来完成这项工作:一个是用户应在其中输入内容的窗口,另一个是全屏透明且始终位于顶部的窗口,这将阻止用户访问另一个窗口。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。