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

2024-03-29 08:02:03 发布

您现在位置:Python中文网/ 问答频道 /正文

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秒后自动关闭。单击按钮将加载一个对话框。单击对话框上的按钮将打开另一个对话框。在打开最后一个对话框之前,它可以正常工作。在

^{pr2}$

Tags: core程序过程错误wxpython模式情况原因
2条回答

可靠地优雅地关闭所有模式对话框的唯一方法是使用wxModalDialogHook记住所有打开的对话框,然后在退出应用程序之前按相反的顺序(即后进先出)关闭所有模式对话框。在

不幸的是,我不知道wxModalDialogHook在Python中是否可用。在

我认为这里发生的是,对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()

相关问题 更多 >