[wxPython-users] Can somebody tell me whats wrong with this code

Robin Dunn robin at alldunn.com
Mon Feb 11 12:56:00 PST 2008


Thippana, Prasoonadevi wrote:
> I am not able to use events with these id wx.ID_OK and wx.ID_CANCEL.
>  
>  
> import wx
>  
> class MessageDialog(wx.Dialog):
>     def __init__(self,parent,id,title):
>         wx.Dialog.__init__(self,parent,id,title)
>  
>         self.vt = wx.BoxSizer(wx.VERTICAL)
>  
>         self.stext1 = wx.StaticText(self,11,'Close the window')
>         self.vt.AddSpacer(40)
>         self.vt.Add(self.stext1,0,wx.ALIGN_CENTER  )
>         self.vt.AddSpacer(40)
>         self.sizer = self.CreateButtonSizer(wx.OK | wx.CANCEL)
>         self.vt.Add(self.sizer,0,wx.ALIGN_CENTER )
>         self.SetSizer(self.vt)
>         self.Bind(wx.EVT_BUTTON,self.OnOk,id =wx.ID_OK)
>         self.Bind(wx.EVT_BUTTON,self.OnCancel,id =wx.ID_CANCEL)
>         self.ShowModal()
>         self.Destory()
>  
>     def OnOk(self,event):
>         self.Close()
>  
>     def OnCancel(self,event):
>         self.Close()
>        
> app = wx.App()
> dlg = MessageDialog(None, -1,'Error')
> dlg.Show(True)
>  
> app.MainLoop()


1. if you put a print statement in the OnOk and OnCancel methods you'll 
see that they are being called.

2. You are trying to use the dialog both modally and non-modally.  Which 
do you want?

3a. If modal, then using the Close method is not the right thing to do. 
  Use EndModal instead, passing the value that should be returned from 
the ShowModal call.  (Normally it is the ID of the button whose event 
handler is calling EndModal.)

3b. If non-modal, then since it is a dialog you'll also want to have a 
handler for the EVT_CLOSE event that calls self.Destroy.  By default 
dialogs are not destroyed when they are closed, so although calling 
self.Close does send the event, the dialog won't get destroyed 
automatically like frames do.


-- 
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!





More information about the wxpython-users mailing list