Multiple GUIs from a central one

67 views
Skip to first unread message

robert....@gmail.com

unread,
Nov 11, 2016, 10:45:11 AM11/11/16
to wxPython-users
Hi all,
let's me explain my scenario.

I have multiple standalone applications, each with its own mainloop and, if I run them separately, it's all ok.

I would like to create a "central window" (in a new file), with a button for  each application, in order to run the selected code in a separate window.
If I do this by executing something like os.system("python app1/app1.py") it works well, but I don't like this way (it isn't so elegant).

I would like to import each app as a module and then run run it from its "internal" function.
Anyway I know it is not possible to have multiple mainloops in one running script.
If I do this, in fact, I get the app window, but then all is freezed (and I think it's "normal", since it isn't the right way to do this).

Do you have any tips about that?

Thanks.
Robert

John Fabiani

unread,
Nov 11, 2016, 10:50:59 AM11/11/16
to wxpython-users
Take a look at the menu system for wxPython.  That is how it works.

--
You received this message because you are subscribed to the Google Groups "wxPython-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wxpython-users+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Matt Newville

unread,
Nov 11, 2016, 11:33:33 AM11/11/16
to wxpytho...@googlegroups.com
Robert,


The way I handle this is to use a single wx.App (your "central window"), but then create and show instances of wx.Frames that correspond to the daughter apps.   That is, if the wx.App for each of your applications is simply creating and showing a wx.Frame instance, then your single global application ought to be able to create and show instances of those wx.Frames when needed too, without making a separate wx.App.

There can sometimes be slightly tricky things to handle, such as what "EVT_CLOSE" is meant to do, but I find that having wx.Frames that can be created and destroyed independently is better anyway.

I don't know if that approach would work for you,

--Matt Newville

robert....@gmail.com

unread,
Nov 12, 2016, 11:54:28 AM11/12/16
to wxPython-users, newv...@cars.uchicago.edu
Thanks for replying!

I did as you suggested:
I get the window but then it's all freezed.
It looks like the events are not "transmitted back".

Here there is my code:

import wx

class InitialMenu(wx.Dialog):
   
def __init__(self, parent, id, title):
        wx
.Dialog.__init__(self, parent, id, title, size=(300, 200))

       
self.colour = wx.Colour(0, 0, 0)
       
        sorter_btn
= wx.Button(self, 1, 'App1', (115, 25))
        sorter_btn
.Bind(wx.EVT_BUTTON, self.onApp1Button)

        viewer_btn
= wx.Button(self, 2, 'App2', (115, 80))
        viewer_btn
.Bind(wx.EVT_BUTTON, self.onApp2Button)

       
self.Centre()
       
self.ShowModal()
       
self.Destroy()


   
def onApp1Button(self, event):
       
print("Starting App1!")

       
import app1
       
        app1_frame
= app1.createFrame(None, -1, "App1", size=(500, 500)) # it is a 'class createFrame(wx.Frame)'
        app1_frame
.Show()

   
   
def onApp2Button(self, event):
       
""" For now just print a message"""
       
print("Starting App2!")


   
app
= wx.App(False)
InitialMenu(None, -1, 'Select app')
app
.MainLoop()

Any idea?
Thanks again!

Robert

Tim Roberts

unread,
Nov 12, 2016, 11:11:36 PM11/12/16
to wxpytho...@googlegroups.com
On Nov 12, 2016, at 8:54 AM, robert....@gmail.com wrote:
>
> Thanks for replying!
>
> I did as you suggested:
> I get the window but then it's all freezed.
> It looks like the events are not "transmitted back".

Yes. Do you understand why? You are creating a modal dialog box. Your call to self.ShowModal() will not return until the dialog closes, so by the time you get around to calling app.MainLoop, the main window is gone. Without the main loop, there's nobody who can process the messages for the other frame.

You either need to make this a modeless dialog, or use a wx.Frame for the main program.

Tim Roberts, ti...@probo.com
Providenza & Boekelheide, Inc.

robert....@gmail.com

unread,
Nov 13, 2016, 12:01:22 PM11/13/16
to wxPython-users
Thanks!
Yes, now the "main widnow" is a frame and it works.

I have just a litte problem:
when I close the app1 window, also the "main window" is closed.

The app1 window is not a main window's child (at least I didn'nt set) so, as far as I know, the close event should not be propagated until the main window.

Am I wrong?
Thanks a lot!

Robert

Tim Roberts

unread,
Nov 14, 2016, 11:27:32 AM11/14/16
to wxpytho...@googlegroups.com
robert....@gmail.com wrote:
>
> Yes, now the "main widnow" is a frame and it works.
>
> I have just a litte problem:
> when I close the app1 window, also the "main window" is closed.
>
> The app1 window is not a main window's child (at least I didn'nt set)
> so, as far as I know, the close event should not be propagated until
> the main window.

You haven't shown us the code in "app1", but as I understand it, this
was it's own application. Remember that "import" will essentially
execute the whole file. Are you creating yet another instance of wx.App
and starting another main loop? If you don't have that stuff inside an
'if __name__=="__main__" ' statement, that could be the issue.

--

robert....@gmail.com

unread,
Nov 16, 2016, 12:59:04 PM11/16/16
to wxPython-users
Thanks for the tips!
I fixed the problem :)
Basically I didn't manage the EVT_CLOSE...I did it and it works now!
I have some other little points, but in case I'll open a separate discussion on that.

Thank you very much!
Robert
Reply all
Reply to author
Forward
0 new messages