开发者

How do I safely destroy a dialog window of a wxPython application?

I created a wxPython application which shows some messages on a dialog window. The dialog window is needed to be force-destroyed by the application before I click the dialog OK button. I used wx.lib.delayedresult to make 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 desire to do while I got a error message below:

(python:15150): Gtk-CRITICAL **: gtk_widget_开发者_如何学Godestroy: assertion `GTK_IS_WIDGET (widget)' failed

How do I "safely" destroy a dialog without clicking the dialog button?


It has been a while since I have used wxWidgets but I think your dlg.Destroy() may be in the wrong place. Try moving it into the main thread.

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


I would use a 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()

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


My problem with dlg.Destroy() is that it is not exiting the prompt. I have done following to exit the prompt:

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 move to next line.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜