【问题标题】:wxPython - Automatically closing nested modal dialogswxPython - 自动关闭嵌套的模式对话框
【发布时间】:2023-04-09 17:31:01
【问题描述】:

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()

【问题讨论】:

  • 我没有运行它...但是尝试覆盖self.Close,并将self.closeOpenDialogs() 移动到您的新关闭方法中...我猜您是从线程内部调用self.closeOpenDialogs .. .而且你永远不应该从线程内部更新 gui ......真的,如果父级被销毁,它应该销毁所有子级

标签: python wxpython wxwidgets


【解决方案1】:

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

【讨论】:

  • 这很好用 :) 使用 Show 而不是 ShowModal 是一个不错的技巧
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-03-22
    • 1970-01-01
    • 2018-04-11
    • 2012-04-05
    • 1970-01-01
    • 2013-07-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多