Can somebody tell me whats wrong with this code

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

Thippana, Prasoonadevi wrote:

I am not able to use events with these id wx.ID_OK and wx.ID_CANCEL.

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!

Add
event.Skip()
to your two event handlers under the self.Close() lines.
And change self.Destory() to self.Destroy()

···

On Feb 11, 2008 1:56 PM, Thippana, Prasoonadevi pthippan@mc.com 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()

I used EndModal() and it worked.Thanks everybody

···

-----Original Message-----
From: Robin Dunn [mailto:robin@alldunn.com]
Sent: Monday, February 11, 2008 3:56 PM
To: wxPython-users@lists.wxwidgets.org
Subject: Re: [wxPython-users] Can somebody tell me whats wrong with this
code

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!

---------------------------------------------------------------------
To unsubscribe, e-mail: wxPython-users-unsubscribe@lists.wxwidgets.org
For additional commands, e-mail: wxPython-users-help@lists.wxwidgets.org