Separate instances from multiple button click

44 views
Skip to first unread message

robert....@gmail.com

unread,
Nov 17, 2016, 5:15:20 AM11/17/16
to wxPython-users
Hi all,
in my "main gui" I have a button that creates an object containing a separate application in a separate frame.
The application, natively, uses messages to comunicate among its classes.
If I click just once, there is no problem.

If I click twice (or more) I get another windows that basically are "mirrors" of the first one (as far as I understand they share messages, variables and so on).
Of course I don't want something like that, I want to have separate instances of that class.

Any idea?
Should I use thread?

Thank you very much.
Robert

Tim Roberts

unread,
Nov 17, 2016, 1:02:23 PM11/17/16
to wxpytho...@googlegroups.com
robert....@gmail.com wrote:
>
> in my "main gui" I have a button that creates an object containing a
> separate application in a separate frame.

In this kind of circumstance you need to be rather precise with your
language. If this is all part of one process, then there is only one
application. There is one wx.App object, and one main loop. You can
certainly have multiple top-level windows hanging off of a single
application, and I'm guessing that what you really have.


> The application, natively, uses messages to comunicate among its classes.
> If I click just once, there is no problem.
>
> If I click twice (or more) I get another windows that basically are
> "mirrors" of the first one (as far as I understand they share
> messages, variables and so on).
> Of course I don't want something like that, I want to have separate
> instances of that class.

It depends. When you click again, do you create a new instance of the
other window? If so, then that window has its own class object and its
own variables, and it only processes messages that are aimed for that
window.

When you say "messages", what do you mean? Have you implemented your
own messaging architecture? If you are doing a simple
publish/subscribe, where all subscribers see all messages that are
published, then you might have the problem you describe. But if you are
using wxPython messaging, then each message has a "target", and they
won't get confused.

If you have done your own messaging system, and you don't have the
concept of a "target" for the message, then you may need to add that.

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

robert....@gmail.com

unread,
Nov 17, 2016, 1:18:22 PM11/17/16
to wxPython-users


Il giorno giovedì 17 novembre 2016 19:02:23 UTC+1, Tim Roberts ha scritto:
robert....@gmail.com wrote:
>
> in my "main gui" I have a button that creates an object containing a
> separate application in a separate frame.

In this kind of circumstance you need to be rather precise with your
language.  If this is all part of one process, then there is only one
application.  There is one wx.App object, and one main loop.  You can
certainly have multiple top-level windows hanging off of a single
application, and I'm guessing that what you really have.


Yes, sorry for the confusion.
I have a single wx.App and multiple frames.


> The application, natively, uses messages to comunicate among its classes.
> If I click just once, there is no problem.
>
> If I click twice (or more) I get another windows that basically are
> "mirrors" of the first one (as far as I understand they share
> messages, variables and so on).
> Of course I don't want something like that, I want to have separate
> instances of that class.

It depends.  When you click again, do you create a new instance of the
other window?  If so, then that window has its own class object and its
own variables, and it only processes messages that are aimed for that
window.
 
I create a new instance.
 

When you say "messages", what do you mean?  Have you implemented your
own messaging architecture?  If you are doing a simple
publish/subscribe, where all subscribers see all messages that are
published, then you might have the problem you describe.  But if you are
using wxPython messaging, then each message has a "target", and they
won't get confused.
 
I'm using wx.lib.pubsub.pub version 1 and, honestly, I don't have the concept of target.
 

If you have done your own messaging system, and you don't have the
concept of a "target" for the message, then you may need to add that.
 
Thank you very much!
 

Tim Roberts

unread,
Nov 17, 2016, 1:29:33 PM11/17/16
to wxpytho...@googlegroups.com
robert....@gmail.com wrote:
>
> I'm using wx.lib.pubsub.pub version 1 and, honestly, I don't have the
> concept of target.

Do you get what I mean there? If you have a message like
"display.update", then you might add an "instance ID" in the message, so
you send "0.display.update" or "1.display.update". That way, the
application that is instance #0 can ignore messages for other instances.

Note that I just made this up -- it's possible that pubsub might have
this kind of thing built-in. I don't know the package very well.

DevPlayer

unread,
Nov 17, 2016, 8:01:23 PM11/17/16
to wxPython-users
Like Tim say, you can create top level topics based on each top-level window. Although I thought topic names needed to start with an alpha character and not zero through nine. This is likely the simplest solution.

Or, you can also create multiple publishers. I use version 3 of pubsub and not so much  wx.lib.pubsub.pub.
Typical usage is:

from pubsub import pub

However you can looking into using:

instance0_pub = pubsub.Publisher(...)
instance1_pub
= pubsub.Publisher(...)

You'll have to figure out the arguments for the Publisher class.

Provided here for quick reference:

Tim Roberts

unread,
Nov 18, 2016, 1:16:36 AM11/18/16
to wxpytho...@googlegroups.com
On Nov 17, 2016, at 5:01 PM, DevPlayer <devp...@gmail.com> wrote:
>
> Like Tim say, you can create top level topics based on each top-level window. Although I thought topic names needed to start with an alpha character and not zero through nine. This is likely the simplest solution.

Sorry -- I didn't know the details of pubsub.

robert....@gmail.com

unread,
Nov 18, 2016, 4:52:08 AM11/18/16
to wxPython-users
Wow! Many thanks to all...it looks like I have a lot of things to study!
In the meantime that I find the way to "delivery" the messages in the right way, how can I disable the button for all the time that the frame is opened?
I mean: I click on the button, the frame is opened and the button is disable until I don't close the frame (this to avoid multiple instances that share the messages in the wrong manner).

Really many thanks!
Best

Robert

DevPlayer

unread,
Nov 20, 2016, 7:24:41 PM11/20/16
to wxPython-users
class SecondFrame(wx.Frame):
   
def onClose(self, event):
        pub
.sendMessage('button.enable', options=None)
       
event.Skip()


class FirstFrame(wx.Frame):
   
def __init__(self, ...):
       
self.Bind(wx.BUTTON, onClick, self.Button)
        pub
.subscribe(self.enableButton, 'button.enable')
       
   
def onClick(self):
       
if function_launch_second_frame():
           
self.Button.Disable()
       
   
def enableButton(self):
       
self.Button.Enable()



Dev Player

unread,
Nov 21, 2016, 3:57:41 AM11/21/16
to wxpython-users
Reply all
Reply to author
Forward
0 new messages