Passing variables among split window panels

I’ve modified Mike’s split window example in an attempt to
open a text to a ListCtrl.

I’m trying to pass self.dirname to the right panel’s csv
reader to open. There is a ton of ways to “pass variables between classes”.

I’m
trying to use the global method in the main window. However all my attempts have failed. Passing variables between functions is one thing.

Passing variables between instances of classes is making my head spin.
Any help would be
appreciated.

csv_splitter_try.py (5.07 KB)

road.txt (559 Bytes)

Hi,

I’ve modified Mike’s split window example in an attempt to
open a text to a ListCtrl.

I’m trying to pass self.dirname to the right panel’s csv
reader to open. There is a ton of ways to “pass variables between classes”.

I’m
trying to use the global method in the main window. However all my attempts have failed. Passing variables between functions is one thing.

Passing variables between instances of classes is making my head spin.
Any help would be
appreciated.

Personally, I don’t like to pass variables between classes because it can be such a pain. And when you refactor the code and put the classes in separate files, it can get more complicated. So instead, I usually use pubsub. Whenever I need to send one class a piece of data, I send (publish) it to the other class, which will have a listener attached to it. Thus in your case, I would create two separate listeners, one for each class. Then I would publish to each of them as needed.

You can read about how to do this sort of thing here:

http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/ (wxPython 2.8)

Or here for wxPython 2.9:

There are also several articles on the wxPython wiki on this subject.

  • Mike
···

On Thursday, October 3, 2013 11:05:24 AM UTC-5, George McCown wrote:

Good tutorials, Mike!

···

On Thu, Oct 3, 2013 at 12:19 PM, Mike Driscoll kyosohma@gmail.com wrote:

Hi,

On Thursday, October 3, 2013 11:05:24 AM UTC-5, George McCown wrote:

I’ve modified Mike’s split window example in an attempt to
open a text to a ListCtrl.

I’m trying to pass self.dirname to the right panel’s csv
reader to open. There is a ton of ways to “pass variables between classes”.

I’m
trying to use the global method in the main window. However all my attempts have failed. Passing variables between functions is one thing.

Passing variables between instances of classes is making my head spin.
Any help would be
appreciated.

Personally, I don’t like to pass variables between classes because it can be such a pain. And when you refactor the code and put the classes in separate files, it can get more complicated. So instead, I usually use pubsub. Whenever I need to send one class a piece of data, I send (publish) it to the other class, which will have a listener attached to it. Thus in your case, I would create two separate listeners, one for each class. Then I would publish to each of them as needed.

You can read about how to do this sort of thing here:

http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/ (wxPython 2.8)

Or here for wxPython 2.9:

http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/

There are also several articles on the wxPython wiki on this subject.

  • Mike

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/groups/opt_out.

Thanks! One of these days, I’ll have to learn the topic tree thing that Werner mentioned and write about that as well.

Mike

···

On Thursday, October 3, 2013 11:38:55 AM UTC-5, schoenborno wrote:

Good tutorials, Mike!

print str(LPanel.self.class_jump.GetValue())
it should be:

···

Hi George,

  On 03/10/2013 18:05, George McCown wrote:
            I’ve

modified Mike’s split window example in an attempt to
open a text to a ListCtrl.

            I’m

trying to pass self.dirname to the right panel’s csv
reader to open. There is a ton of ways to “pass
variables between classes”.

           I’m
            trying to use the global method in the

main window. However all my
attempts have failed. Passing variables between
functions is one thing.

            Passing

variables between instances of classes is making my head spin.
Any help would be
appreciated.

    Mike's suggestion of using

pubsub is a very good option.

      I              f you feel it is overkill at

this point , here some pointers related to
your code sample.

            In MyForm you have this:

print str(LPanel.class_jump.GetValue())
In your LeftPanel.OnChooseRoot method you define "self.class_jump", 'self' at this point is pointing to 'LeftPanel'.
If you then want to get at "LPanel.class_jump" in e.g.your RightPanel class you would have to go 'via' the parent, at which point things get ugly.
this line:
info = csv.reader(open(self.class_jump, "rb"))
would then need to be:
info = csv.reader(open(self.GetParent().leftP.class_jump, "rb"))
But the above line assumes that in MyForm you define 'leftP' like this:
self.leftP = LeftPanel(splitter)
As you can see this is getting a bit messy that is why Mike's suggestion is much better and cleaner.
BTW, a good way to get a handle on these things, at least it worked and still works, for me is to use a debugger and step through the code. With that you would easily see what "self" is referring to in different parts of your code.
Following explains "self" etc a lot better then I could.
Hope this helped and didn't just add to the confusion.
Werner

:wink: http://www.diveintopython.net/object_oriented_framework/defining_classes.html

Your right werner. That method is messy. I have successfully use Mike’s suggestion using subpub. When I print msg.data I see my pathname in the right panel.

However, I need to stop the MainFrame loop before loading filename in the left panel. Since this is a split window both panels need legitimate instances. I need

to find a way to default (empty rows) the right panel and then use subpub. Perhaps I could default to a blank right panel??? If I’ll do some resarch on that.

···

On Thursday, October 3, 2013 11:19:06 AM UTC-5, Mike Driscoll wrote:

Hi,

On Thursday, October 3, 2013 11:05:24 AM UTC-5, George McCown wrote:

I’ve modified Mike’s split window example in an attempt to
open a text to a ListCtrl.

I’m trying to pass self.dirname to the right panel’s csv
reader to open. There is a ton of ways to “pass variables between classes”.

I’m
trying to use the global method in the main window. However all my attempts have failed. Passing variables between functions is one thing.

Passing variables between instances of classes is making my head spin.
Any help would be
appreciated.

Personally, I don’t like to pass variables between classes because it can be such a pain. And when you refactor the code and put the classes in separate files, it can get more complicated. So instead, I usually use pubsub. Whenever I need to send one class a piece of data, I send (publish) it to the other class, which will have a listener attached to it. Thus in your case, I would create two separate listeners, one for each class. Then I would publish to each of them as needed.

You can read about how to do this sort of thing here:

http://www.blog.pythonlibrary.org/2010/06/27/wxpython-and-pubsub-a-simple-tutorial/ (wxPython 2.8)

Or here for wxPython 2.9:

http://www.blog.pythonlibrary.org/2013/09/05/wxpython-2-9-and-the-newer-pubsub-api-a-simple-tutorial/

There are also several articles on the wxPython wiki on this subject.

  • Mike

Hi George,

···

On 03/10/2013 22:48, George McCown wrote:

Your right werner. That method is messy. I have successfully use Mike's suggestion using subpub. When I print msg.data I see my pathname in the right panel.
However, I need to stop the MainFrame loop before loading filename in the left panel. Since this is a split window both panels need legitimate instances. I need
to find a way to default (empty rows) the right panel and then use subpub. Perhaps I could default to a blank right panel??? If I'll do some resarch on that.

The main loop will only stop when you close your app.

To me it would make sense to have the panel(s), listctrl(s) etc blank until you select the data (csv or whatever) and when that happens you fill the control(s) with the relevant data.

Werner

I’m almost there. subpub is passing self.dirname ok …, but I suspect the right panel will not allow instances to def createAndLayout(self): since it is set up to parent=parent.
There is no error on traceback and I can’t ask that class to print any instances.

csv_splitter_try.py (5.29 KB)

road.txt (559 Bytes)

···

On Thursday, October 3, 2013 4:24:43 PM UTC-5, werner wrote:

Hi George,

On 03/10/2013 22:48, George McCown wrote:

Your right werner. That method is messy. I have successfully use
Mike’s suggestion using subpub. When I print msg.data I see my
pathname in the right panel.

However, I need to stop the MainFrame loop before loading filename in
the left panel. Since this is a split window both panels need
legitimate instances. I need

to find a way to default (empty rows) the right panel and then use
subpub. Perhaps I could default to a blank right panel??? If I’ll do
some resarch on that.

The main loop will only stop when you close your app.

To me it would make sense to have the panel(s), listctrl(s) etc blank
until you select the data (csv or whatever) and when that happens you
fill the control(s) with the relevant data.

Werner

I’m not 100% sure but maybe a AttributeError.

You send a string (msg = str(self.dirname + ‘\’ + self.filename)) and in the listener you try to access msg.data.

Hi George,

I'm almost there. subpub is passing self.dirname ok ..., but I suspect the right panel will not allow instances to def createAndLayout(self): since it is set up to parent=parent.
There is no error on traceback and I can't ask that class to print any instances.

I changed your code around to us the 'new/latest' pubsub API which is included with wxPython 2.9.5 and has been available for some time if one got it from the pypubsub site as an independent download.

One does not have to do it but I define the topics in a topic tree module/file, this gives me code completion for topics (for me much less error prone) and it also provides 'auto' documentation for your topics and their parameters.

Hope this is useful
Werner

csv_topics.py (424 Bytes)

csv_splitter_try.py (5.47 KB)

···

On 05/10/2013 18:36, George McCown wrote:

If you run the script, and open a text delimiter file you will see splitter_try_traceback6.txt (outputed in the same
directory as the script), prints the dirname fine. The listener is working. My brain is stalled when that instance
can not pass to the next function.

···

On Sunday, October 6, 2013 3:41:29 AM UTC-5, Torsten wrote:

I’m not 100% sure but maybe a AttributeError.

You send a string (msg = str(self.dirname + ‘\’ + self.filename)) and in the listener you try to access msg.data.

Stick with me werner. I’ve been hopping between 2.8 and 2.9.

In 2.8 is get "‘module’ object has no attribute ‘addTopicDefnProvider’’

Using 2.9:

your csv.topics.py is a kind-of singleton class. Where I need
to set the parameters in that mod correct? The subpud examples at the web site
has a similar example as yours. I can’t find any working examples.

I now realize the main panel will not open since I’m passing a instance that’s out of the loop.
The right panel needs the directory file. I want the right panel to show a blank column list (as it does at run), you
open a file, and wham! there’s the data in the column list.

So ideally… the subpub reciever sees the mesage and def createAndLayout(self) and switches self.sortedlist =
to self.sortedlist = [ ‘the open formatted list’ ].

Please help me understand my fundamental issue.

···

On Sunday, October 6, 2013 8:25:04 AM UTC-5, werner wrote:

Hi George,

On 05/10/2013 18:36, George McCown wrote:

I’m almost there. subpub is passing self.dirname ok …, but I
suspect the right panel will not allow instances to def
createAndLayout(self): since it is set up to parent=parent.

There is no error on traceback and I can’t ask that class to print any
instances.

I changed your code around to us the ‘new/latest’ pubsub API which is
included with wxPython 2.9.5 and has been available for some time if one
got it from the pypubsub site as an independent download.

One does not have to do it but I define the topics in a topic tree
module/file, this gives me code completion for topics (for me much less
error prone) and it also provides ‘auto’ documentation for your topics
and their parameters.

Hope this is useful

Werner

BTW, I ordered wxpython in action just now. Nothing like a good paperback to carry around.

···

On Sunday, October 6, 2013 7:20:09 PM UTC-5, George McCown wrote:

Stick with me werner. I’ve been hopping between 2.8 and 2.9.

In 2.8 is get "‘module’ object has no attribute ‘addTopicDefnProvider’’

Using 2.9:

your csv.topics.py is a kind-of singleton class. Where I need
to set the parameters in that mod correct? The subpud examples at the web site
has a similar example as yours. I can’t find any working examples.

I now realize the main panel will not open since I’m passing a instance that’s out of the loop.
The right panel needs the directory file. I want the right panel to show a blank column list (as it does at run), you
open a file, and wham! there’s the data in the column list.

So ideally… the subpub reciever sees the mesage and def createAndLayout(self) and switches self.sortedlist =
to self.sortedlist = [ ‘the open formatted list’ ].

Please help me understand my fundamental issue.

On Sunday, October 6, 2013 8:25:04 AM UTC-5, werner wrote:

Hi George,

On 05/10/2013 18:36, George McCown wrote:

I’m almost there. subpub is passing self.dirname ok …, but I
suspect the right panel will not allow instances to def
createAndLayout(self): since it is set up to parent=parent.

There is no error on traceback and I can’t ask that class to print any
instances.

I changed your code around to us the ‘new/latest’ pubsub API which is
included with wxPython 2.9.5 and has been available for some time if one
got it from the pypubsub site as an independent download.

One does not have to do it but I define the topics in a topic tree
module/file, this gives me code completion for topics (for me much less
error prone) and it also provides ‘auto’ documentation for your topics
and their parameters.

Hope this is useful

Werner

Hi George,

···

On 07/10/2013 02:53, George McCown wrote:

BTW, I ordered wxpython in action just now. Nothing like a good paperback to carry around.

A very good decission!

Werner

Hi George,

Stick with me werner. I've been hopping between 2.8 and 2.9.

As you are starting something new I would definitely recommend 2.9, and best would be 2.9.5. This will provide you the newer pubsub stuff and much much much move and it contains a ton of bug fixes you would not get with 2.8.x.

If you are on 2.9 the move to 3.0 will be easier and the same will be true to move to Phoenix when it comes out.

In 2.8 is get "'module' object has no attribute 'addTopicDefnProvider''

I don't remember when that was added, it might have been in 2.8 already BUT only if one set pubsub to run with its API 3, in 2.8 pubsub by default runs with API 1.

Using 2.9:

your csv.topics.py is a kind-of singleton class. Where I need
to set the parameters in that mod correct? The subpud examples at the web site
has a similar example as yours. I can't find any working examples.

That file just defines the valid messages and for each message you define the parameters to be passed around in its "msgDataSpec" method, e.g.:

def msgDataSpec(dirname, filename):

This defines that we have to pass a 'dirname' and a 'filename'.

I have attached a changed sample where I added my 'data' message class which has different message to deal with the 'data' state in my application.

See here for more info:
http://pubsub.sourceforge.net/usage/usage_advanced_maintain.html#specify-topic-tree-def

I now realize the main panel will not open since I'm passing a instance that's out of the loop.

Note sure what you mean here.

The right panel needs the directory file. I want the right panel to show a blank column list (as it does at run), you
open a file, and wham! there's the data in the column list.

So, your listener has to load the data into the list.

So ideally.. the subpub reciever sees the mesage and def createAndLayout(self) and switches self.sortedlist =
to self.sortedlist = [ 'the open formatted list' ].

I changed so the right list is filled when opening the file.

Please help me understand my fundamental issue.

Do a diff of the attached against your code and it should help you understand what you missed.

I would probably do the following to clean this up some more.

- better separation of 'duties', e.g. currently your 'createAndLayout' does some stuff which is not really needed at that point and is duplicated in the method I added. Maybe change 'createAndLayout' to 'createListCtrl' and really only create the empty list and it could call 'reloadList', but in your case I don't think there ever is data so not really needed.
- be consistent in your naming of methods etc, e.g. 'GetListCtrl' should be 'getListCtrl' - myself I was really really bad in this, getting better with it and I really think it helps a lot when I later on look at code.
- the listctrl does not indicate the sortorder when one clicks on the column header - look at the demo for this (a little tip - ImageList)

Hope this helps
Werner

csv_topics.py (2.29 KB)

csv_splitter_try.py (5.99 KB)

···

On 07/10/2013 02:20, George McCown wrote:

This is good. Adding self.reloadList() at the receive is what I needed. It works for wxpython 2.7 Tonight I’ll use my wxpython 2.9 machine to learn your topics. This is real good werner.

···

On Monday, October 7, 2013 1:59:32 AM UTC-5, werner wrote:

Hi George,

On 07/10/2013 02:20, George McCown wrote:

Stick with me werner. I’ve been hopping between 2.8 and 2.9.

As you are starting something new I would definitely recommend 2.9, and
best would be 2.9.5. This will provide you the newer pubsub stuff and
much much much move and it contains a ton of bug fixes you would not get
with 2.8.x.

If you are on 2.9 the move to 3.0 will be easier and the same will be
true to move to Phoenix when it comes out.

In 2.8 is get "‘module’ object has no attribute ‘addTopicDefnProvider’’

I don’t remember when that was added, it might have been in 2.8 already
BUT only if one set pubsub to run with its API 3, in 2.8 pubsub by
default runs with API 1.

Using 2.9:

your csv.topics.py is a kind-of singleton class. Where I need

to set the parameters in that mod correct? The subpud examples at the
web site

has a similar example as yours. I can’t find any working examples.

That file just defines the valid messages and for each message you
define the parameters to be passed around in its “msgDataSpec” method, e.g.:

def msgDataSpec(dirname, filename):

This defines that we have to pass a ‘dirname’ and a ‘filename’.

I have attached a changed sample where I added my ‘data’ message class
which has different message to deal with the ‘data’ state in my application.

See here for more info:

http://pubsub.sourceforge.net/usage/usage_advanced_maintain.html#specify-topic-tree-def

I now realize the main panel will not open since I’m passing a
instance that’s out of the loop.

Note sure what you mean here.

The right panel needs the directory file. I want the right panel to
show a blank column list (as it does at run), you

open a file, and wham! there’s the data in the column list.

So, your listener has to load the data into the list.

So ideally… the subpub reciever sees the mesage and def
createAndLayout(self) and switches self.sortedlist =

to self.sortedlist = [ ‘the open formatted list’ ].

I changed so the right list is filled when opening the file.

Please help me understand my fundamental issue.

Do a diff of the attached against your code and it should help you
understand what you missed.

I would probably do the following to clean this up some more.

  • better separation of ‘duties’, e.g. currently your ‘createAndLayout’
    does some stuff which is not really needed at that point and is
    duplicated in the method I added. Maybe change ‘createAndLayout’ to
    ‘createListCtrl’ and really only create the empty list and it could call
    ‘reloadList’, but in your case I don’t think there ever is data so not
    really needed.

  • be consistent in your naming of methods etc, e.g. ‘GetListCtrl’ should
    be ‘getListCtrl’ - myself I was really really bad in this, getting
    better with it and I really think it helps a lot when I later on look at
    code.

  • the listctrl does not indicate the sortorder when one clicks on the
    column header - look at the demo for this (a little tip - ImageList)

Hope this helps

Werner