Having my own App subclass

I want to do something I haven't done before and I want you to tell me
if I'm even in the right direction.

In my app I want to have the possibility of having multiple frames.
Think about Word or something, where you can open multiple documents
in multiple separate frames.

I've been thinking that a good way to do this would be to (a) subclass
`PySimpleApp` to my own `App` class, (b) have that app keep a list of
frames, (c) have every frame keep a reference to the main app.

Does this make sense?

Ram.

···

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Ram,

I want to do something I haven't done before and I want you to tell me
if I'm even in the right direction.

In my app I want to have the possibility of having multiple frames.
Think about Word or something, where you can open multiple documents
in multiple separate frames.

I've been thinking that a good way to do this would be to (a) subclass
`PySimpleApp` to my own `App` class, (b) have that app keep a list of
frames, (c) have every frame keep a reference to the main app.

Do these frames need to know anything about each other? You could just
use subprocess to kick off a new instance of your program

···

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Oh, that’s smart, I didn’t think of that. That would cause them to work on different cores of the CPU, which would be highly desirable.

I’m still not sure whether this should be done with multiprocessing or subprocess, I’ll think about it.

Ram.

···

On Tue, Apr 27, 2010 at 3:45 PM, Steven Sproat sproaty@gmail.com wrote:

Hi Ram,

I want to do something I haven’t done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.

Think about Word or something, where you can open multiple documents

in multiple separate frames.

I’ve been thinking that a good way to do this would be to (a) subclass

PySimpleApp to my own App class, (b) have that app keep a list of

frames, (c) have every frame keep a reference to the main app.

Do these frames need to know anything about each other? You could just

use subprocess to kick off a new instance of your program

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Ram,

I want to do something I haven’t done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.

Think about Word or something, where you can open multiple documents

in multiple separate frames.

I’ve been thinking that a good way to do this would be to (a) subclass

PySimpleApp to my own App class, (b) have that app keep a list of

frames, (c) have every frame keep a reference to the main app.

I do exactly this, and it works well. (c) doesn’t appear to be necessary in practice, because you can just use wx.GetApp() from anywhere in the program. There may be some overhead associated with this that I don’t know about, but I haven’t noticed it.

Do these frames need to know anything about each other? You could just

use subprocess to kick off a new instance of your program

Oh, that’s smart, I didn’t think of that. That would cause them to work on different cores of the CPU, which would be highly desirable.

I’m still not sure whether this should be done with multiprocessing or subprocess, I’ll think about it.

I find the multiprocessing module very useful for firing off non-graphical tasks, but I would be careful about using it for anything involving the GUI directly. On Unix, it simply calls os.fork(), which will (if I understand it correctly) clone the current Python process, wx.App and all, and this probably won’t break but it may have unintended side effects. On Windows, I think it actually needs to launch a new Python interpreter and send any necessary data as pickle objects, which excludes wxPython classes, so you might as well just use subprocess for that.

-Nat

···

On Tue, Apr 27, 2010 at 7:05 AM, cool-RR cool-rr@cool-rr.com wrote:

On Tue, Apr 27, 2010 at 3:45 PM, Steven Sproat sproaty@gmail.com wrote:

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Is there a demo of the first two? I couldn’t find one. How can I learn more about them?

Ram.

···

On Tue, Apr 27, 2010 at 2:38 PM, GadgetSteve@live.co.uk wrote:


From: “cool-RR” ram.rachum@gmail.com

Sent: Tuesday, April 27, 2010 12:28 PM

To: “wxPython-users” wxpython-users@googlegroups.com

Subject: [wxPython-users] Having my own App subclass

I want to do something I haven’t done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.

Think about Word or something, where you can open multiple documents

in multiple separate frames.

I’ve been thinking that a good way to do this would be to (a) subclass

PySimpleApp to my own App class, (b) have that app keep a list of

frames, (c) have every frame keep a reference to the main app.

Does this make sense?

Ram.

Of course you could always use the wxPython built in classes wx.MDIParentFrame and wx.MDIChildFrame

or you could use wx.aui.AuiMDIParentFrame take a look at the demo for examples.

Gadget/Steve

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Hi Ram,

I want to do something I haven’t done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.

Think about Word or something, where you can open multiple documents

in multiple separate frames.

I’ve been thinking that a good way to do this would be to (a) subclass

PySimpleApp to my own App class, (b) have that app keep a list of

frames, (c) have every frame keep a reference to the main app.

I do exactly this, and it works well. (c) doesn’t appear to be necessary in practice, because you can just use wx.GetApp() from anywhere in the program. There may be some overhead associated with this that I don’t know about, but I haven’t noticed it.

Thanks for that Nat, I didn’t know it and it sounds like a better idea than me keeping a reference to the app myself.

Do these frames need to know anything about each other? You could just

use subprocess to kick off a new instance of your program

Oh, that’s smart, I didn’t think of that. That would cause them to work on different cores of the CPU, which would be highly desirable.

I’m still not sure whether this should be done with multiprocessing or subprocess, I’ll think about it.

I find the multiprocessing module very useful for firing off non-graphical tasks, but I would be careful about using it for anything involving the GUI directly. On Unix, it simply calls os.fork(), which will (if I understand it correctly) clone the current Python process, wx.App and all, and this probably won’t break but it may have unintended side effects. On Windows, I think it actually needs to launch a new Python interpreter and send any necessary data as pickle objects, which excludes wxPython classes, so you might as well just use subprocess for that.

-Nat

Interesting. I will try to figure this out.

But wait, I do use the multiprocessing module a lot for doing non-graphical tasks, but from the wxPython GUI. Does that mean that when I do it on Linux, it will fork and clone the App?

Ram.

···

On Tue, Apr 27, 2010 at 6:20 PM, Nathaniel Echols nathaniel.echols@gmail.com wrote:

On Tue, Apr 27, 2010 at 7:05 AM, cool-RR cool-rr@cool-rr.com wrote:

On Tue, Apr 27, 2010 at 3:45 PM, Steven Sproat sproaty@gmail.com wrote:

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

    Of course you could always use the wxPython built in classes
    wx.MDIParentFrame and wx.MDIChildFrame
    or you could use wx.aui.AuiMDIParentFrame

Please don't -- MDI is an abomination, and even MS has abandoned it.

Just my not so humble opinion.

-Chris

···

--
Christopher Barker, Ph.D.
Oceanographer

Emergency Response Division
NOAA/NOS/OR&R (206) 526-6959 voice
7600 Sand Point Way NE (206) 526-6329 fax
Seattle, WA 98115 (206) 526-6317 main reception

Chris.Barker@noaa.gov

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Apparently so - try the appended code. However, creating new GUI elements in the child process causes a segfault on my Mac. I didn’t try doing anything else with the app. Anyway, in my experience (on Mac and Linux - I don’t do Windows) having that forked wx.App hanging around doesn’t cause any problems, and the child process still exits when the target function is finished. In one of the non-graphical modules I use the multiprocessing module independently for some “embarrassingly parallel” calculations, so the GUI is forking a process which then forks as many more as I want (i.e. # of CPUs), and this all seems to work too without weird side effects. (The drawback to doing it this way is that if the GUI crashes or freezes, all of the calculations crash too, so I also have an option of using subprocess instead, albeit with less interactivity. But all of the GUI stuff is still happening in a single process.)

-Nat

import wx

import multiprocessing

def child_process () :

app = wx.GetApp()

print type(app)

def child_process2 () :

app = wx.GetApp()

frame2 = wx.Frame(None, -1, “Hello again!”)

panel2 = wx.Panel(frame2, -1, size=(640,480))

txt2 = wx.StaticText(panel2, -1, “Hello again!”, pos=(200,200))

frame2.Fit()

frame2.Show()

def OnStartProcess (evt) :

p = multiprocessing.Process(target=child_process)

p.start()

print “Process started”

def OnStartProcess2 (evt) :

p = multiprocessing.Process(target=child_process2)

p.start()

print “Process 2 started”

if name == “main” :

app = wx.App(0)

frame = wx.Frame(None, -1, “Hello, world!”)

panel = wx.Panel(frame, -1, size=(640,480))

txt = wx.StaticText(panel, -1, “Hello, world!”, pos=(200,200))

btn = wx.Button(panel, -1, “Start process”, pos=(200,240))

btn2 = wx.Button(panel, -1, “Start process with frame”, pos=(200,280))

frame.Bind(wx.EVT_BUTTON, OnStartProcess, btn)

frame.Bind(wx.EVT_BUTTON, OnStartProcess2, btn2)

frame.Fit()

frame.Show()

app.MainLoop()

···

On Tue, Apr 27, 2010 at 10:00 AM, cool-RR cool-rr@cool-rr.com wrote:

But wait, I do use the multiprocessing module a lot for doing non-graphical tasks, but from the wxPython GUI. Does that mean that when I do it on Linux, it will fork and clone the App?

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

Yes. That is a good approach if that is what makes sense to you. (Although I would probably just subclass wx.App and not worry about wx.PySimpleApp.)

There are also the wx.lib.docview and wx.lib.pydocview modules if you want something more advanced using the standard Doc/View pattern. There are samples for them under the samples folder.

···

On 4/27/10 4:28 AM, cool-RR wrote:

I want to do something I haven't done before and I want you to tell me
if I'm even in the right direction.

In my app I want to have the possibility of having multiple frames.
Think about Word or something, where you can open multiple documents
in multiple separate frames.

I've been thinking that a good way to do this would be to (a) subclass
`PySimpleApp` to my own `App` class, (b) have that app keep a list of
frames, (c) have every frame keep a reference to the main app.

Does this make sense?

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en


From: “cool-RR” ram.rachum@gmail.com
Sent: Tuesday, April 27, 2010 12:28 PM
To: “wxPython-users” wxpython-users@googlegroups.com
Subject: [wxPython-users] Having my own App subclass

I want to do something I haven't done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.
Think about Word or something, where you can open multiple documents
in multiple separate frames.

I've been thinking that a good way to do this would be to (a) subclass
`PySimpleApp` to my own `App` class, (b) have that app keep a list of
frames, (c) have every frame keep a reference to the main app.

Does this make sense?

Ram.

Of course you could always use the wxPython built in classes wx.MDIParentFrame and wx.MDIChildFrame
or you could use wx.aui.AuiMDIParentFrame take a look at the demo for examples.
Gadget/Steve

Is there a demo of the first two? I couldn’t find one. How can I learn more about them?

In the wxPython demo application, just search for MDI

Ram.

···

From: cool-RR

Sent: Tuesday, April 27, 2010 5:55 PM

To: wxPython-users ; GadgetSteve

Subject: Re: [wxPython-users] Having my own App subclass

On Tue, Apr 27, 2010 at 2:38 PM, GadgetSteve@live.co.uk wrote:


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en


To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks Robin.

I checked them out, and it seems they provide some useful tools, but I don’t want to have all my documents under one main window. Each “document” is a pretty heavy simulation with lots of windows, so I think each should have its own frame.

Thanks,

Ram.

···

On Tue, Apr 27, 2010 at 8:19 PM, Robin Dunn robin@alldunn.com wrote:

On 4/27/10 4:28 AM, cool-RR wrote:

I want to do something I haven’t done before and I want you to tell me

if I’m even in the right direction.

In my app I want to have the possibility of having multiple frames.

Think about Word or something, where you can open multiple documents

in multiple separate frames.

I’ve been thinking that a good way to do this would be to (a) subclass

PySimpleApp to my own App class, (b) have that app keep a list of

frames, (c) have every frame keep a reference to the main app.

Does this make sense?

Yes. That is a good approach if that is what makes sense to you. (Although I would probably just subclass wx.App and not worry about wx.PySimpleApp.)

There are also the wx.lib.docview and wx.lib.pydocview modules if you want something more advanced using the standard Doc/View pattern. There are samples for them under the samples folder.

Robin Dunn

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

I consulted with someone, and he said I shouldn’t worry about the wx.App getting replicated: Linux has something called Copy-On-Write which will assure that there will be only one copy of the wx.App in memory, as long as the second process does not try to modify it.

My question now: In my program I coded a function that, when the user presses “New…” and the current frame already has a simulation in it, starts a multiprocessing.Process which start a new wx.App and a new wx.Frame inside it.

Will this be problematic on Linux? I mean, the second process will have two wx.Apps in memory!

Ram.

···

On Tue, Apr 27, 2010 at 7:21 PM, Nathaniel Echols nathaniel.echols@gmail.com wrote:

On Tue, Apr 27, 2010 at 10:00 AM, cool-RR cool-rr@cool-rr.com wrote:

But wait, I do use the multiprocessing module a lot for doing non-graphical tasks, but from the wxPython GUI. Does that mean that when I do it on Linux, it will fork and clone the App?

Apparently so - try the appended code. However, creating new GUI elements in the child process causes a segfault on my Mac. I didn’t try doing anything else with the app. Anyway, in my experience (on Mac and Linux - I don’t do Windows) having that forked wx.App hanging around doesn’t cause any problems, and the child process still exits when the target function is finished. In one of the non-graphical modules I use the multiprocessing module independently for some “embarrassingly parallel” calculations, so the GUI is forking a process which then forks as many more as I want (i.e. # of CPUs), and this all seems to work too without weird side effects. (The drawback to doing it this way is that if the GUI crashes or freezes, all of the calculations crash too, so I also have an option of using subprocess instead, albeit with less interactivity. But all of the GUI stuff is still happening in a single process.)

-Nat

import wx

import multiprocessing

def child_process () :

app = wx.GetApp()

print type(app)

def child_process2 () :

app = wx.GetApp()

frame2 = wx.Frame(None, -1, “Hello again!”)

panel2 = wx.Panel(frame2, -1, size=(640,480))

txt2 = wx.StaticText(panel2, -1, “Hello again!”, pos=(200,200))

frame2.Fit()

frame2.Show()

def OnStartProcess (evt) :

p = multiprocessing.Process(target=child_process)

p.start()

print “Process started”

def OnStartProcess2 (evt) :

p = multiprocessing.Process(target=child_process2)

p.start()

print “Process 2 started”

if name == “main” :

app = wx.App(0)

frame = wx.Frame(None, -1, “Hello, world!”)

panel = wx.Panel(frame, -1, size=(640,480))

txt = wx.StaticText(panel, -1, “Hello, world!”, pos=(200,200))

btn = wx.Button(panel, -1, “Start process”, pos=(200,240))

btn2 = wx.Button(panel, -1, “Start process with frame”, pos=(200,280))

frame.Bind(wx.EVT_BUTTON, OnStartProcess, btn)

frame.Bind(wx.EVT_BUTTON, OnStartProcess2, btn2)

frame.Fit()

frame.Show()

app.MainLoop()

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

My guess is that yes, this will cause a problem. You should not do anything related to the GUI in the forked processes, just like you would not do anything GUI-related in a background thread. If you need a child process to have its own GUI then use subprocess to spawn a completely new process.

···

On 4/28/10 1:40 AM, cool-RR wrote:

My question now: In my program I coded a function that, when the user
presses "New..." and the current frame already has a simulation in it,
starts a `multiprocessing.Process` which start a new `wx.App` and a new
`wx.Frame` inside it.

Will this be problematic on Linux? I mean, the second process will have
two `wx.App`s in memory!

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

DocView also supports that as the SDI mode.

···

On 4/28/10 1:32 AM, cool-RR wrote:

    There are also the wx.lib.docview and wx.lib.pydocview modules if
    you want something more advanced using the standard Doc/View
    pattern. There are samples for them under the samples folder.
    --
    Robin Dunn

Thanks Robin.

I checked them out, and it seems they provide some useful tools, but I
don't want to have all my documents under one main window. Each
"document" is a pretty heavy simulation with lots of windows, so I think
each should have its own frame.

--
Robin Dunn
Software Craftsman

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Thanks. I’m starting to think that multiprocessing will not be good for this, and that I better use subprocess. Does anyone know a sane guide to subprocess? The official documentation is desperately complex, and the Popen class takes like 20 arguments.

Ram.

···

On Wed, Apr 28, 2010 at 7:09 PM, Robin Dunn robin@alldunn.com wrote:

On 4/28/10 1:40 AM, cool-RR wrote:

My question now: In my program I coded a function that, when the user

presses “New…” and the current frame already has a simulation in it,

starts a multiprocessing.Process which start a new wx.App and a new

wx.Frame inside it.

Will this be problematic on Linux? I mean, the second process will have

two wx.Apps in memory!

My guess is that yes, this will cause a problem. You should not do anything related to the GUI in the forked processes, just like you would not do anything GUI-related in a background thread. If you need a child process to have its own GUI then use subprocess to spawn a completely new process.

Robin Dunn

To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com

or visit http://groups.google.com/group/wxPython-users?hl=en

cool-RR wrote:

Thanks. I'm starting to think that `multiprocessing` will not be good
for this, and that I better use `subprocess`. Does anyone know a sane
guide to `subprocess`? The official documentation is desperately
complex, and the `Popen` class takes like 20 arguments.

def is_exe():
    try:
        x = sys.frozen
        return True
    except AttributeError:
        return False

def open_window():
    program = ('python', os.path.abspath(sys.argv[0]))
    if is_exe():
        program = os.path.abspath(sys.argv[0]) # haven't tested this yet

    subprocess.Popen(program)

···

--
Steven Sproat, BSc
http://www.launchpad.net/whyteboard

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

Unless you are on Mac, where the MDI implementation allows you to have "true" Mac apps. You know, the kind where when you close the last window, the app keeps on running, and the system menu does the right thing.

Paul

···

On 4/27/10 10:16 AM, Chris Barker wrote:

Of course you could always use the wxPython built in classes
wx.MDIParentFrame and wx.MDIChildFrame
or you could use wx.aui.AuiMDIParentFrame

Please don't -- MDI is an abomination, and even MS has abandoned it.

Just my not so humble opinion.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

And unless you are on Windows and you are a reservoir/well engineer or
a geologist, in which case there are about 1,000 modern-looking apps
written in .NET 4 still using the MDI approach, because it makes much
more sense than any other implementation in this small and niche
field.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.alice.it/infinity77/

==> Never *EVER* use RemovalGroup for your house removal. You'll
regret it forever.
The Doomed City: Removal Group: the nightmare <==

···

On 28 April 2010 22:56, Paul McNett wrote:

On 4/27/10 10:16 AM, Chris Barker wrote:

Of course you could always use the wxPython built in classes
wx.MDIParentFrame and wx.MDIChildFrame
or you could use wx.aui.AuiMDIParentFrame

Please don't -- MDI is an abomination, and even MS has abandoned it.

Just my not so humble opinion.

Unless you are on Mac, where the MDI implementation allows you to have
"true" Mac apps. You know, the kind where when you close the last window,
the app keeps on running, and the system menu does the right thing.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en

I find that all my Windows users tend to prefer MDI. I prefer it, too, actually. For my apps, I turn MDI on for Windows and Mac, and off for Linux. If some Windows user ever complains about the MDI, I'll make it a configuration option but so far it's just what everyone still prefers.

Paul

···

On 4/28/10 3:05 PM, Andrea Gavana wrote:

On 28 April 2010 22:56, Paul McNett wrote:

On 4/27/10 10:16 AM, Chris Barker wrote:

  Of course you could always use the wxPython built in classes
wx.MDIParentFrame and wx.MDIChildFrame
or you could use wx.aui.AuiMDIParentFrame

Please don't -- MDI is an abomination, and even MS has abandoned it.

Just my not so humble opinion.

Unless you are on Mac, where the MDI implementation allows you to have
"true" Mac apps. You know, the kind where when you close the last window,
the app keeps on running, and the system menu does the right thing.

And unless you are on Windows and you are a reservoir/well engineer or
a geologist, in which case there are about 1,000 modern-looking apps
written in .NET 4 still using the MDI approach, because it makes much
more sense than any other implementation in this small and niche
field.

--
To unsubscribe, send email to wxPython-users+unsubscribe@googlegroups.com
or visit http://groups.google.com/group/wxPython-users?hl=en