[wxPython] wxPy fundamentals, specifying location of disembodied dialog

A few questions about the following small program:

1. Why does commenting out the myApp line cause the program to fail/crash? I can see no place that the wxApp object is 'connected' to anything else.

2. In order to get the file dialog shown in a screen location other than top left corner, I need to create a wxFrame in a specified location, but it doesn't seem to matter whether or not I make that frame the parent of the dialog or not! Why is that?

2a. How can I determine the parameters to locate the frame centred on the screen, and more importantly, where in the wxWindows help file is this information found (I'm presuming that it's there)?

3. Why is the frame destruction call needed to prevent memory leaks? I'd have thought python's garbage collection should have taken care of that.

4. Is there anything else I should know about this? The idea here is to build a utility that opens files and does stuff with them, but doesn't need/want any GUI beyond that.

thanks,
Shi.
-=0=-

from wxPython.wx import *

myApp = wxPySimpleApp()
frame = wxFrame(None, -1, "Minimal", pos=(300,200))

# this works same with parent as either frame or None
fwin = wxFileDialog(None, "Load File(s)", "", "", "*.*", wxOPEN)
fwin.ShowModal()

files = fwin.GetFilenames()
frame.Destroy()

Furthermore,

The values of the pos tuple (used in construction of the wxFrame) seem to make no difference in the location of the file open dialog - what's going on??

Shi Sherebrin wrote:

···

A few questions about the following small program:

1. Why does commenting out the myApp line cause the program to fail/crash? I can see no place that the wxApp object is 'connected' to anything else.

2. In order to get the file dialog shown in a screen location other than top left corner, I need to create a wxFrame in a specified location, but it doesn't seem to matter whether or not I make that frame the parent of the dialog or not! Why is that?

2a. How can I determine the parameters to locate the frame centred on the screen, and more importantly, where in the wxWindows help file is this information found (I'm presuming that it's there)?

3. Why is the frame destruction call needed to prevent memory leaks? I'd have thought python's garbage collection should have taken care of that.

4. Is there anything else I should know about this? The idea here is to build a utility that opens files and does stuff with them, but doesn't need/want any GUI beyond that.

thanks,
Shi.
-=0=-

from wxPython.wx import *

myApp = wxPySimpleApp()
frame = wxFrame(None, -1, "Minimal", pos=(300,200))

# this works same with parent as either frame or None
fwin = wxFileDialog(None, "Load File(s)", "", "", "*.*", wxOPEN)
fwin.ShowModal()

files = fwin.GetFilenames()
frame.Destroy()

Shi Sherebrin wrote:

A few questions about the following small program:

1. Why does commenting out the myApp line cause the program to fail/crash? I can see no place that the wxApp object is 'connected' to anything else.

Because there is a fair amount of library initialization that takes place when the wxApp is constructed. Depending on the platform you'll also run into things that require an app object to function correctly.

2. In order to get the file dialog shown in a screen location other than top left corner, I need to create a wxFrame in a specified location, but it doesn't seem to matter whether or not I make that frame the parent of the dialog or not! Why is that?

On MS Windows the common system dialogs are given the handle of one of the wxFrames as parent if no parent is specified. It's needed by Windows for some reason.

2a. How can I determine the parameters to locate the frame centred on the screen, and more importantly, where in the wxWindows help file is this information found (I'm presuming that it's there)?

>
> Furthermore,
> The values of the pos tuple (used in construction of the wxFrame) seem
> to make no difference in the location of the file open dialog - what's
> going on??

The Center or CenterOnScreen methods of wxWindow should do it for you. Be aware however that you are unable to set the position of the common system dialgs on MS Windows, because they are not really derived from wxDialog but are just wrappers around an API call.

3. Why is the frame destruction call needed to prevent memory leaks? I'd have thought python's garbage collection should have taken care of that.

In most cases the destruction of a wxPython object does not cause the destruction of the wrapped C++ object. In the case of windows this is because the Python object does not own the C++ object, its parent window does. wxFrames will gracefully mark themselves for destruction when the Close method is called (and the EVT_CLOSE event handler allows it to continue) or, like wxDialogs, when the Destroy method is called. Even then it is not deleted right away but after all pending events have been processed. That's why you need to call app.MainLoop even when all you have is a dialog so the window can be destroyed after the first time through the loop, then since there are no more top-level windows MainLoop will exit.

···

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