wxPython - 自动关闭嵌套模式对话框

wxPython - Automatically closing nested modal dialogs

Python2.7,WxPython3.0.2

我们正在尝试在特定条件下自动关闭整个程序。由于种种原因,我们不能直接杀掉进程。我们已经取得了一定程度的成功。如果没有模态对话框,或者只有一个模态对话框,我们可以关闭它。一旦我们引入第二个模态对话框(嵌套),它就无法正常停止。

收到的实际错误似乎是:

wx._core.PyAssertionError: C++ assertion "IsRunning()" failed at ..\..\src\common\evtloopcmn.cpp(83) in wxEventLoopBase::Exit(): Use ScheduleExit() on not running loop

这是我们问题的一个工作示例。框架将在 5 秒后自动关闭。单击该按钮将加载一个对话框。单击对话框上的按钮将打开另一个对话框。在打开最后一个对话框之前它工作正常。

from threading import Thread
from time import sleep
import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="TEST", size=(400, 400))
        self.Show()
        self.__someDialog = None
        self.__myThread = None

        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)

        self.__myThread = Thread(target=self.__waitThenClose, name="Closer")
        self.__myThread.setDaemon(True)
        self.__myThread.start()

    def __onOK(self, evt):
        self.__someDialog = SomeDialog(self)
        self.__someDialog.ShowModal()

    def closeOpenDialogs(self):
        lst = wx.GetTopLevelWindows()

        for i in range(len(lst) - 1, 0, -1):
            if isinstance(lst[i], wx.Dialog):
                print "Closing " + str(lst[i])
                lst[i].Close(True)
                #lst[i].Destroy()

    def __waitThenClose(self):

        for x in range(0, 5):
            print "Sleeping..."
            sleep(1)

        self.closeOpenDialogs()
        wx.CallAfter(self.Close, True)


class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Some Dialog')
        self.SetSize((300, 300))
        self.__anotherDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")

        self.Bind(wx.EVT_BUTTON, self.__onOK)
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __onOK(self, evt):
        self.__anotherDialog = AnotherDialog(self)
        self.__anotherDialog.ShowModal()

    def __on_btn_cancel(self, event):
        self.EndModal(wx.ID_CANCEL)


class AnotherDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, None, id=-1, title='Another Dialog')
        self.SetSize((200, 200))
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __on_btn_cancel(self, event):
        self.EndModal(wx.ID_CANCEL)


if __name__ == "__main__":

    app = wx.App()

    mainFrame = MainFrame()

    app.MainLoop()

我认为这里发生的是对 ShowModal() 的第一次调用在应用程序级别(不仅仅是框架级别)阻止了,这阻止了第二个对话框完全初始化。要解决此问题,我会调用 Show() 而不是 ShowModal() 并将 wx.FRAME_FLOAT_ON_PARENT 添加到对话框样式标志。您还可以在对话框打开时对不希望用户与之交互的程序部分调用 Disable()

编辑:这是一个工作示例:

from threading import Thread
from time import sleep
import wx


class MainFrame(wx.Frame):
    def __init__(self):
        wx.Frame.__init__(self, None, title="TEST", size=(400, 400))
        self.Show()
        self.__someDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)

        self.__myThread = Thread(target=self.__waitThenClose, name="Closer")
        self.__myThread.setDaemon(True)
        self.__myThread.start()

    def __onOK(self, evt):
        self.__someDialog = SomeDialog(self)
        self.__someDialog.ShowModal()

    def closeOpenDialogs(self, evt=None):
        lst = wx.GetTopLevelWindows()
        for i in range(len(lst) - 1, 0, -1):
            dialog = lst[i]
            if isinstance(dialog, wx.Dialog):
                print "Closing " + str(dialog)
                # dialog.Close(True)
                wx.CallAfter(dialog.Close)
                # sleep(1)
                # dialog.Destroy()

    def __waitThenClose(self):
        for x in range(0, 10):
            print "Sleeping..."
            sleep(1)
        wx.CallAfter(self.closeOpenDialogs)
        wx.CallAfter(self.Close, True)


class SomeDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Some Dialog')
        self.SetSize((300, 300))
        self.__anotherDialog = None
        self.__okButton = wx.Button(self, -1, "Press me")
        self.Bind(wx.EVT_BUTTON, self.__onOK)
        wx.EVT_CLOSE(self, self.__on_btn_cancel)

    def __onOK(self, evt):
        self.__anotherDialog = AnotherDialog(self)
        self.__anotherDialog.SetWindowStyleFlag(
            wx.FRAME_FLOAT_ON_PARENT|wx.DEFAULT_DIALOG_STYLE)
        self.__anotherDialog.Show()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.EndModal(wx.ID_CANCEL)


class AnotherDialog(wx.Dialog):
    def __init__(self, parent):
        wx.Dialog.__init__(self, parent, id=-1, title='Another Dialog')
        self.SetSize((200, 200))
        wx.EVT_CLOSE(self, self.__on_btn_cancel)
        parent.Disable()

    def __on_btn_cancel(self, event):
        event.Skip()
        self.GetParent().Enable()
        # self.EndModal(wx.ID_CANCEL)


if __name__ == "__main__":
    app = wx.App()
    mainFrame = MainFrame()
    app.MainLoop()

无论是否通过您自己的代码显式打开了所有模态对话框,可靠而优雅地关闭它们的唯一方法是使用 wxModalDialogHook 记住所有打开的对话框,然后将它们全部关闭,在退出应用程序之前的相反(即后进先出)顺序。

很遗憾,我不知道 wxModalDialogHook 在 Python 中是否可用。