Windows MainLoop terminating prematurely

Hi everyone,
This is my first post here, so please bear with me if I'm doing
anything wrong. If this question has been asked before, I apologize
for reposting!

I'm using python 2.7, and wxpython 2.8, both are for x64 windows 7.

I am updating the contents of my wx.Frame by destroying it and re-
initializing it. I'm doing this from a thread that is called when I
press a button. Basically, the button press calls a long-running-
thread that, when completed, updates the GUI.

In linux, the following works fine, but in Windows, the frame is
redrawn for only a fraction of a second, which leads me to believe
that MainLoop is terminating:

#long running thread
myframe.Destroy()
myframe = MainFrame(args)

myframe is my single, top level Frame & MainFrame is a wxFrame class
that I have defined. I understand that MainLoop terminates when all
top level frames have been destroyed. However, the following code
results in the same behavior:

#long running thread
tempframe = MainFrame(args)
app.SetTopWindow(tempframe)

myframe.Destroy()
myframe = MainFrame(args)

Both windows are erased as soon as Destroy() is called.
What is the best way to work around this? Ideally, I'd prefer if the
program worked on both linux and windows.

Thanks a million,
Varun

Varun wrote:

I am updating the contents of my wx.Frame by destroying it and re-
initializing it. I'm doing this from a thread that is called when I
press a button. Basically, the button press calls a long-running-
thread that, when completed, updates the GUI.

That's not a good plan. Destroying your main window typically sends a
WM_QUIT message to the application, which is the trigger to exit the
main loop and exit the application.

Also, remember that it's not safe to update your GUI from a secondary
thread. You need to send a message of some kind to wake the GUI thread
and allow it to update itself.

Both windows are erased as soon as Destroy() is called.
What is the best way to work around this?

The best way is not to destroy your main window. Why are you doing
that? If you want, you can delete all the controls on the window and
recreate them, but that is a very, very unusual thing to do. Are you
just trying to update the information that is shown? If so, then can't
you just update some global state, and trigger a method that causes the
frame to update the individual controls? THAT'S closer to the right method.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Hi Tim,
Thanks for the info. I'm trying to update a treelistctrl, adding new
tree elements to it(ie doing a hard drive search, and updating the gui
with new folders as they are found). I didn't know there was a way to
update that without recreating it. If I add an element to the
treelistctrl and do a Refresh(), would that reflect on the gui?

Thanks,
Varun

···

On Jun 21, 4:01 pm, Tim Roberts <t...@probo.com> wrote:

Varun wrote:
> I am updating the contents of my wx.Frame by destroying it and re-
> initializing it. I'm doing this from a thread that is called when I
> press a button. Basically, the button press calls a long-running-
> thread that, when completed, updates the GUI.

That's not a good plan. Destroying your main window typically sends a
WM_QUIT message to the application, which is the trigger to exit the
main loop and exit the application.

Also, remember that it's not safe to update your GUI from a secondary
thread. You need to send a message of some kind to wake the GUI thread
and allow it to update itself.

> Both windows are erased as soon as Destroy() is called.
> What is the best way to work around this?

The best way is not to destroy your main window. Why are you doing
that? If you want, you can delete all the controls on the window and
recreate them, but that is a very, very unusual thing to do. Are you
just trying to update the information that is shown? If so, then can't
you just update some global state, and trigger a method that causes the
frame to update the individual controls? THAT'S closer to the right method.

--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

Varun wrote:

Thanks for the info. I'm trying to update a treelistctrl, adding new
tree elements to it(ie doing a hard drive search, and updating the gui
with new folders as they are found). I didn't know there was a way to
update that without recreating it. If I add an element to the
treelistctrl and do a Refresh(), would that reflect on the gui?

Certainly. That's how you build the control in the first place, isn't
it? You can call AddColumn or AppendItem any time you want. Refresh is
the right answer, too; that marks the window as "dirty", so it will be
repainted the next time your message queue goes empty.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.

Thanks Tim. Now I'm wondering how to clear the screen of some
controls. I need to be able to do a second search following the first,
and any deleted files need to be removed from the GUI. Is there a good
way to do this?

Thanks,
Varun

···

On Jun 21, 4:55 pm, Tim Roberts <t...@probo.com> wrote:

Varun wrote:
> Thanks for the info. I'm trying to update a treelistctrl, adding new
> tree elements to it(ie doing a hard drive search, and updating the gui
> with new folders as they are found). I didn't know there was a way to
> update that without recreating it. If I add an element to the
> treelistctrl and do a Refresh(), would that reflect on the gui?

Certainly. That's how you build the control in the first place, isn't
it? You can call AddColumn or AppendItem any time you want. Refresh is
the right answer, too; that marks the window as "dirty", so it will be
repainted the next time your message queue goes empty.

--
Tim Roberts, t...@probo.com
Providenza & Boekelheide, Inc.

Varun wrote:

Thanks Tim. Now I'm wondering how to clear the screen of some
controls. I need to be able to do a second search following the first,
and any deleted files need to be removed from the GUI. Is there a good
way to do this?

The wx.TreeListCtrl has a DeleteRoot method that will delete the root
node and all of its children. Is that enough?

If you have certain controls that should only be shown in certain
circumstances, it's almost always better to create them and manage their
visibility with Show and Hide.

···

--
Tim Roberts, timr@probo.com
Providenza & Boekelheide, Inc.