如何在tkinter中创建模态对话框?

Mac*_*Mac 4 python mfc tkinter python-3.x

我有一个运行一些嵌入式Python脚本的MFC应用程序.我试图使这个嵌入式脚本创建模态的对话框之一,但我没有取得多大成功.

有人能指出我制作模态对话的方法吗?我是否需要使用Windows函数或仅使用Tk或Python函数就足够了?

对于我用Google搜索的内容,看起来以下功能组合应该具有魔力,但它们似乎不像我期望的那样工作:

focus_set()

grab_set()

transient(parent)
Run Code Online (Sandbox Code Playgroud)

Bry*_*ley 10

grab_set是制作窗口"应用模式"的正确机制.也就是说,它需要来自同一应用程序中所有其他窗口的所有输入(即:同一进程中的其他Tkinter窗口),但它允许您与其他应用程序进行交互.

如果您希望对话框是全局模态的,请使用grab_set_global.这将接管整个系统的所有键盘和鼠标输入.使用它时必须非常小心,因为如果您有一个阻止您的应用程序释放抓取的错误,您可以轻松地将自己锁定在计算机之外.

当我需要这样做时,在开发过程中我将尝试编写一个防弹故障保护措施,例如定时器,它将在一段固定的时间后释放抓取.


Joe*_*CCP 5

在我的一个项目中,我在父窗口上使用了 Tcl 窗口管理器属性“-disabled”,该属性称为(模式)顶层对话框窗口。

不知道您在 MFC 应用程序中显示的哪些窗口是通过 Tcl 内容创建或使用的,但如果您的父窗口是基于 Tk 的,您可以这样做:

在 Python 中,只需在顶层窗口的创建方法中调用父窗口即可:

MyParentWindow.wm_attributes("-disabled", True)
Run Code Online (Sandbox Code Playgroud)

当您通过模态窗口获得所需的内容后,请不要忘记在模态窗口内使用回调函数,以再次启用父窗口上的输入!(否则您将无法再次与父窗口交互!):

MyParentWindow.wm_attributes("-disabled", False)
Run Code Online (Sandbox Code Playgroud)

Tkinter(Tcl 版本 8.6)Python 示例(在 Windows 10 64 位上测试):

# Python 3+
import tkinter as tk
from tkinter import ttk

class SampleApp(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.minsize(300, 100)
        self.button = ttk.Button(self, text="Call toplevel!", command=self.Create_Toplevel)
        self.button.pack(side="top")

    def Create_Toplevel(self):

        # THE CLUE
        self.wm_attributes("-disabled", True)

        # Creating the toplevel dialog
        self.toplevel_dialog = tk.Toplevel(self)
        self.toplevel_dialog.minsize(300, 100)

        # Tell the window manager, this is the child widget.
        # Interesting, if you want to let the child window 
        # flash if user clicks onto parent
        self.toplevel_dialog.transient(self)



        # This is watching the window manager close button
        # and uses the same callback function as the other buttons
        # (you can use which ever you want, BUT REMEMBER TO ENABLE
        # THE PARENT WINDOW AGAIN)
        self.toplevel_dialog.protocol("WM_DELETE_WINDOW", self.Close_Toplevel)



        self.toplevel_dialog_label = ttk.Label(self.toplevel_dialog, text='Do you want to enable my parent window again?')
        self.toplevel_dialog_label.pack(side='top')

        self.toplevel_dialog_yes_button = ttk.Button(self.toplevel_dialog, text='Yes', command=self.Close_Toplevel)
        self.toplevel_dialog_yes_button.pack(side='left', fill='x', expand=True)

        self.toplevel_dialog_no_button = ttk.Button(self.toplevel_dialog, text='No')
        self.toplevel_dialog_no_button.pack(side='right', fill='x', expand=True)

    def Close_Toplevel(self):

        # IMPORTANT!
        self.wm_attributes("-disabled", False) # IMPORTANT!

        self.toplevel_dialog.destroy()

        # Possibly not needed, used to focus parent window again
        self.deiconify() 


if __name__ == "__main__":
    app = SampleApp()
    app.mainloop()
Run Code Online (Sandbox Code Playgroud)

有关 Tcl 窗口管理器属性的更多信息,只需查看 Tcl 文档: https: //wiki.tcl.tk/9457