[Tutor] how to call "~wxDialog()" in python?

Jeff Shannon jeff@ccvcorp.com
Tue Jul 22 12:35:01 2003


Thomas CLive Richards wrote:

> when trying to destroy a (WxWindows) window, I have to call a function
> called "self.~wxDialog()" However, python complains about the '~'
> character:
>
> thomi@Julie:~/work/python sources/wxPython$ ./wedderburn.py
> File "./wedderburn.py", line 146
> EVT_BUTTON(self,ID_PREFS_CLOSEBUTTON,self.~wxDialog)
> ^
> SyntaxError: invalid syntax


You're looking at the wxWindows docs, which are for C++.  As of yet, 
there aren't any wxPython-specific docs, just annotations in the C++ 
docs.  So, in many cases, it's necessary to translate the C++ in the 
docs into Python.  

In this particular case, the ~wxDialog() is a C++ destructor.  C++ 
requires you to specify exactly how to destroy an object and free any 
memory that it's allocated; in most cases Python handles that sort of 
thing for you.  So, you really *don't* need to call a destructor for 
Python.  The method that you need to call in order to destroy a wxPython 
window (whether frame, dialog, or whatever) is Destroy().  However, you 
probably want to do that from an OnClose() handler.  In your __init__() 
(or wherever you're hooking events), you'll want to add a line to hook 
EVT_CLOSE:

        EVT_CLOSE(self, self.OnCloseWindow)

The event handler for it is pretty simple:

    def OnCloseWindow(self, event):
        self.Destroy()
        return true

Now, anything that causes your window to close should go through this 
event, which is triggered by the Close() method on all wxWindows.  Thus, 
your above button event looks like this:

        EVT_BUTTON(self,ID_PREFS_CLOSEBUTTON,self.Close())

The real advantage here comes when there's something that needs done any 
time that you close the window; you can put that code in 
OnCloseWindow(), and it will be run regardless of the method used to 
close the window, whether it's by a button, from a menu, or from the 
close-button (the [X]) in the corner of the title bar.

Jeff Shannon
Technician/Programmer
Credit International