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

时间:2015-12-15 18:58:41

标签: python wxpython wxwidgets

Python 2.7,WxPython 3.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()

2 个答案:

答案 0 :(得分:3)

我认为这里发生的事情是第一次调用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.__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, 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()

答案 1 :(得分:0)

可靠地优雅地关闭所有模态对话框的唯一方法是使用wxModalDialogHook来记住所有打开的对话框,然后将它们全部关闭,反之亦然(即LIFO)订单,退出申请前。

不幸的是,我不知道Python中是否有wxModalDialogHook