无模式对话框 tkinter

发布于 2024-12-08 19:15:15 字数 1513 浏览 0 评论 0原文

我正在使用一个名为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦断已成空 2024-12-15 19:15:15

删除显示 self.grab_set() 的行。 “抓取”意味着抓取所有事件并将它们定向到此窗口,这是您不想要的。

如果您更深入地阅读获得代码的网页,您会发现它提到了这一行以及为什么它在那里:

...接下来,构造函数创建对话框主体,然后调用
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:

... Next, the constructor creates the dialog body, and then calls
grab_set to make the dialog modal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文