retrieve values from one function to use in another (wx)

Dear list,

I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.

many thanks

data.txt (256 Bytes)

showcsv.py (3.31 KB)

showcsv2.py (3.3 KB)

···

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.

Hi Carlos,
In the onOpen function, you can make these two values attributes of
the class, by making them self.datalist and self.colnames, and then
createGrid or any function in the class will be able to access them.
Alternately, in onOpen, you could return these values to whatever called
onOpen, then pass them into the call to createGrid. The first way is
simpler; the 2nd way is better in terms of objects being independent
of each other.

I'd recommend not naming the function onOpen...too vague. OnOpenCSV
will make it clearer later on. Also, createGrid is too close to the wx
.CreateGrid().... Again, minor points, but I am all for informative naming.

Che

···

2009/4/14 Carlos "Guâno" Grohmann <carlos.grohmann@gmail.com>:

Dear list,

I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.

Carlos Guâno Grohmann wrote:

Dear list,

I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.

The same way that you would pass data to some other part of the application in any other program. You call a function and depending on who is doing the calling you either pass the data as parameters, or you return it from the function as the return value. Or you can just save the data as attributes of an object and then fetch them from that object later when they are needed.

wxPython also provides the pubsub module for the more complex cases where the function call model requires too much coupling between discreet components. (Your app is not at the point yet, but you could still use this pattern if desired.) With pubsub you can have one or more things in the application that subscribe to a specific topic, and one or more other things in the app that publish messages with that topic.

···

--
Robin Dunn
Software Craftsman
http://wxPython.org Java give you jitters? Relax with wxPython!

Carlos Guâno Grohmann wrote:

Dear list,

I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.
  

See attached (note that to realign the code and change the end of lines - maybe as I am on Windows Vista?) I also hard coded another image for the toolbar - you need to change that back.

I added a self.createGrid call to your onOpen method. Then I changed the createGrid method to accept datalist and colnames as parameters.

One could also change datalist to 'self.datalist' and colnames to 'self.colnames', i.e. save a reference to them and then just use self.datalist in the createGrid method.

Werner

showcsv2.py (3.96 KB)

Thanks, that was what I was looking for.

Carlos

···

On Tue, Apr 14, 2009 at 15:53, Werner F. Bruhin <werner.bruhin@free.fr> wrote:

Carlos Guâno Grohmann wrote:

Dear list,

I'm working my way through python/wxpython trying to make a simple csv
viewer. So far, it works (code attached showcsv.py), and opens the
data (file data.txt) in to a grid. Now I'm trying to separate the grid
creation from the onOpen function, but without success (code
showcsv2.py). My problem is that I don't know how to retrieve the
values of "datalist" and "colnames" from the onOpen function and use
them in the creatGrid function.

See attached (note that to realign the code and change the end of lines -
maybe as I am on Windows Vista?) I also hard coded another image for the
toolbar - you need to change that back.

I added a self.createGrid call to your onOpen method. Then I changed the
createGrid method to accept datalist and colnames as parameters.

One could also change datalist to 'self.datalist' and colnames to
'self.colnames', i.e. save a reference to them and then just use
self.datalist in the createGrid method.

Werner

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

--
Carlos Henrique Grohmann - Geologist D.Sc.
a.k.a. Guano - Linux User #89721
ResearcherID: A-9030-2008
carlos dot grohmann at gmail dot com
http://www.igc.usp.br/pessoais/guano/
_________________
"Good morning, doctors. I have taken the liberty of removing Windows
95 from my hard drive."
--The winning entry in a "What were HAL's first words" contest judged
by 2001: A SPACE ODYSSEY creator Arthur C. Clarke

Can’t stop the signal.