obtain data from a function inside a class

Hi,

Hi,

In this small code, I'd like to obtain the values of x and y. I know that
without the funcion 'wx.Panel', it works, but with it, it doesn't. Do you
know how I could do it?

class values(wx.Panel):
x = 1
def data(self):
y = 5
return(y)

print values().x
print values().data()

Your 'value' class is a sublcass of wx.Panel. Its constructor requires
some arguements. You can't just create it like values(), a panel
requires a parent window as the first arg.

i.e)

frame = wx.Frame(None)
panel = values(frame)

Also

You have defined 'x' as a class variable so you don't have to have
instance of the class object to access it.

i.e)

values.x

data() is a function that requires an instance of the class to be run so

print values(frame).data()

May be helpful: The Python Tutorial — Python 3.13.0 documentation

Cody

···

On Thu, Jan 21, 2010 at 12:31 PM, Ignacio <ignacio.puyol@gmail.com> wrote:

Hi,

Sorry, Imodified the code as you said, to be like this:

class values(wx.Panel):
x = 1
def data(self):
y = 5
return(y)

frame = wx.Frame(None)
panel = values(frame)

print values.x
print values(frame).data()

But it doesn't run.

"Doesn't run" does not mean anything. Did you look at any errors that
were output when you tried to run it?

I am not sure what you are trying to accomplish here as your example
doesn't show any need for creating a Panel.

Anyway, you need to create an App object before creating any other GUI
objects (i.e the frame and any panels). Also if the above code is
exactly what you were trying to run you also need to 'import wx'.

Cody

···

On Thu, Jan 21, 2010 at 1:36 PM, Ignacio <ignacio.puyol@gmail.com> wrote:

Hi, you can modify your code like this :

class test():
     x = 1
     def data(self):
         y = 5
         return y

print test.x
print test().data()

Thank you

Regards

Harya Dananjaya

···

On 22/01/10 09:10, Ignacio wrote:

Yes, of course, my last example was just that, an example, the code I'm doing is much more complex, but that's the part I can't do.

To be more precise, what I'm trying to do is to pass variables between different classes which are in different files, and I'm not able to pass those variables when they are inside a function which is also inside a class.

I don't know if you can view attachments within an email list, but if you can, I've attached two files with a simplified version of the code I'm doing. It tells you exactly what's my problem, and I guess you'll understand why I posted last email.

If you can help, thank you, and if you don't it doesn't matter, I'd use numpy as in here http://wiki.wxpython.org/AnotherTutorial#Plotting.

Hi,

Yes, of course, my last example was just that, an example, the code I'm
doing is much more complex, but that's the part I can't do.

To be more precise, what I'm trying to do is to pass variables between
different classes which are in different files, and I'm not able to pass
those variables when they are inside a function which is also inside a
class.

I don't know if you can view attachments within an email list, but if you
can, I've attached two files with a simplified version of the code I'm
doing. It tells you exactly what's my problem, and I guess you'll understand
why I posted last email.

If you can help, thank you, and if you don't it doesn't matter, I'd use
numpy as in herehttp://wiki.wxpython.org/AnotherTutorial#Plotting.

Have you tried PubSub? I've also heard of people using a Python file
for this where they import the module into all the files that need it
and then modify the variables that way:

<code>
# importable module: passer.py
varOne = "something"
varTwo = "something else"
</code>

<code>
# classOne
import passer
passer.varOne = "123"
</code>

<code>
# classTwo
import passer
print passer.varOne
</code>

Anyway, if these different files are in use at the same time, then
this works. But pubsub is probably better.

···

On Jan 21, 6:10 pm, Ignacio <ignacio.pu...@gmail.com> wrote:

-------------------
Mike Driscoll

Blog: http://blog.pythonlibrary.org

PyCon 2010 Atlanta Feb 19-21 http://us.pycon.org/

I don't understand why you use a text file when you are already
passing x1 to the class PlotPanel1. Passing is the right way to go.

I also don't know why you need to make the plot() and show() calls
into its own module--unless you are later going to make the plot panel
much more elaborate?

Che

···

On Fri, Jan 22, 2010 at 3:15 PM, Ignacio <ignacio.puyol@gmail.com> wrote:

No, but I solved the problem by exporting the info to a txt file an then
reading it to create a graph (which was what I wanted to do). I attach the
to files so you can see how.