How to safely destroy wxPython application dialog box?

I created a wxPython application that shows some messages in a dialog box. The dialog box is necessary for forced destruction by the application before clicking the OK dialog button. I used wx.lib.delayedresult to call the destroy call.

My code is:

import wx
dlg=wx.MessageDialog(somewindow,'somemessage')
from wx.lib.delayedresult import startWorker
def _c(d):
    dlg.EndModal(0)
    dlg.Destroy()
def _w():
    import time
    time.sleep(1.0)
startWorker(_c,_w)
dlg.ShowModal()

This can do what I want to do while I received the error message below:

(python: 15150): Gtk-CRITICAL **: gtk_widget_destroy: statement `GTK_IS_WIDGET (widget) 'failed

How to "safely" destroy a dialog box without clicking a dialog button?

+5
source share
3 answers

, wxWidgets, , dlg.Destroy() . .

import wx
dlg=wx.MessageDialog(somewindow,'somemessage')
from wx.lib.delayedresult import startWorker
def _c(d):
    dlg.EndModal(0)
def _w():
    import time
    time.sleep(1.0)
startWorker(_c,_w)
dlg.ShowModal()
dlg.Destroy()
0

wx.Timer()

import wx

########################################################################
class MyDialog(wx.Dialog):
    """"""

    #----------------------------------------------------------------------
    def __init__(self):
        """Constructor"""
        wx.Dialog.__init__(self, None, title="Test")

        timer = wx.Timer(self)
        self.Bind(wx.EVT_TIMER, self.onTimer, timer)
        timer.Start(5000)

        self.ShowModal()

    #----------------------------------------------------------------------
    def onTimer(self, event):
        """"""
        print "in onTimer"
        self.Destroy()

if __name__ == "__main__":
    app = wx.App(False)
    dlg = MyDialog()
    app.MainLoop()

. http://www.blog.pythonlibrary.org/2009/08/25/wxpython-using-wx-timers/

0

My problem with dlg.Destroy()is that it does not exit the invitation. I did the following to exit the invitation:

def OnCloseWindow(self, e):    
    dial = wx.MessageDialog(None, 'Are you sure to quit?', 'Question',
                            wx.YES_NO | wx.NO_DEFAULT | wx.ICON_QUESTION)
    ret = dial.ShowModal()
    if ret == wx.ID_YES:
        self.Destroy()
        sys.exit(0)

sys.exit(0) will exit the prompt and go to the next line.

0
source

All Articles