无模式对话框 tkinter
我正在使用一个名为 customDialog 的类,基于 http://effbot.org /tkinterbook/tkinter-dialog-windows.htm 标准代码创建一个类,该类是 Toplevel
的子类,并创建模式对话框。 我正在尝试调整代码,以便它也可以创建非模式对话框。标准代码调用 Toplevel
构造函数,创建对话框,然后在最后执行 self.wait_window(self)
,等待对话框窗口关闭。 但我没有成功。
我天真的尝试是简单地跳过 wait_window()
(这是下面我的代码片段中的尝试)。我尝试了其他一些方法,但对话框仍然是模态的,也就是说,我无法与应用程序的主窗口交互。是什么迫使它成为模态的?通常,对话框包含小部件和一些按钮。
class Dialog(Toplevel):
def __init__(self, parent, title = None,modal=True):
Toplevel.__init__(self, parent)
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
#register validators
self.validatePosInt = (body.register(self.OnValidatePosInt),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.initial_focus = self.body(body) #this calls the body function which is overridden, and which draws the dialog
body.grid()
self.buttonbox()
self.grab_set()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
parent.winfo_rooty()+50))
self.initial_focus.focus_set()
if modal:
self.wait_window(self)
I'm using a class called customDialog, based on Based on http://effbot.org/tkinterbook/tkinter-dialog-windows.htm
The standard code creates a class which subclasses Toplevel
, and creates modal dialogs.
I'm trying to adapt the code so it can also create modeless dialogs. The standard code calls the Toplevel
constructor, creates the dialog and then does self.wait_window(self)
at the end, which waits until the dialog window is dismissed.
But I'm not succeeding.
My naive attempt was to simply skip the wait_window()
(that's the attempt in my code snipped below). I've tried a few other things, but the dialog remains modal, that is, I can't interact with the application's main window. What is forcing it to be modal? The dialogs contain widgets and a few buttons, typically.
class Dialog(Toplevel):
def __init__(self, parent, title = None,modal=True):
Toplevel.__init__(self, parent)
self.transient(parent)
if title:
self.title(title)
self.parent = parent
self.result = None
body = Frame(self)
#register validators
self.validatePosInt = (body.register(self.OnValidatePosInt),
'%d', '%i', '%P', '%s', '%S', '%v', '%V', '%W')
self.initial_focus = self.body(body) #this calls the body function which is overridden, and which draws the dialog
body.grid()
self.buttonbox()
self.grab_set()
if not self.initial_focus:
self.initial_focus = self
self.protocol("WM_DELETE_WINDOW", self.cancel)
self.geometry("+%d+%d" % (parent.winfo_rootx()+50,
parent.winfo_rooty()+50))
self.initial_focus.focus_set()
if modal:
self.wait_window(self)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
删除显示
self.grab_set()
的行。 “抓取”意味着抓取所有事件并将它们定向到此窗口,这是您不想要的。如果您更深入地阅读获得代码的网页,您会发现它提到了这一行以及为什么它在那里:
Remove the line that says
self.grab_set()
. A "grab" means to grab all events and direct them toward this window, which you don't want.If you read a bit deeper into the web page where you got the code you'll see that it mentions this exact line and why it is there: