-   Programming (
-   -   python global variable? (

goestin 01-20-2005 07:26 AM

python global variable?
hi all, @ the moment i am working on a piece of code in python.
i'll show you a simplified code of my problem..

from Tkinter import *

class Application(Frame):
        def __init__(self,master=None):

        def x(self):
        def y(self):
                print _x
        def createWidgets(self):
                self.button = Button(self, text="set variable", command=self.x)
                self.button1 = Button(self, text="print variable", command=self.y)
app = Application()

and when i run it, there is a GUI but. when i try to print the variable i get this error:

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/", line 1345, in __call__
return self.func(*args)
File "", line 13, in y
print _x
NameError: global name '_x' is not defined
so i think, the program doesn't remember the variable in de first "def"
and my questions was; is there a way to set a variable in such a way so it can be used thru the whole program? like a global variable i think..

tnx in advance..

btw i'm a python newbie in case you didn't noticed :)


goestin 01-20-2005 07:52 AM

ah already found it :)

global _x
_x = 6



All times are GMT -5. The time now is 04:11 AM.