In all my code that ran on wxPython 2.8, I could close dialogs via the .Close() method, but now in 2.9 it seems that I must use .Destroy as well as .Close(). Is this true?
Below is how I would do it. The dialogs don't close unless I uncomment out the call to Destroy.
import wx
def button(parent, _id, label, event_handler):
"""
Button that's auto-bound to an event
"""
button = wx.Button(parent, _id, label)
button.Bind(wx.EVT_BUTTON, event_handler)
return button
class Frame(wx.Frame):
def __init__(self):
wx.Frame.__init__(self, None)
dialogButton = button(self, wx.NewId(), "Show dialog", lambda evt: ShapeViewer(self).Show())
class ShapeViewer(wx.Dialog):
def __init__(self, gui):
wx.Dialog.__init__(self, gui)
okButton = button(self, wx.ID_OK, "&OK", self.ok)
cancelButton = button(self, wx.ID_CANCEL, "&Cancel", self.cancel)
btnSizer = wx.StdDialogButtonSizer()
btnSizer.AddButton(okButton)
btnSizer.AddButton(cancelButton)
btnSizer.Realize()
sizer = wx.BoxSizer(wx.VERTICAL)
sizer.Add(btnSizer, 0, wx.TOP | wx.BOTTOM | wx.ALIGN_CENTRE, 15)
self.SetSizer(sizer)
self.SetFocus()
self.SetEscapeId(cancelButton.GetId())
self.Bind(wx.EVT_CLOSE, self.on_close)
def ok(self, event):
#self.Destroy()
self.Close()
def cancel(self, event=None):
self.Destroy()
#self.Close()
def on_close(self, event):
event.Skip()
app = wx.App(False)
f = Frame()
f.Show()
app.MainLoop()
···
--
Steven Sproat, BSc
http://www.whyteboard.org/