LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   python global variable? (http://www.linuxquestions.org/questions/programming-9/python-global-variable-280050/)

goestin 01-20-2005 06: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..
Code:

from Tkinter import *

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

        def x(self):
                _x=6
       
        def y(self):
                print _x
       
        def createWidgets(self):
                self.button = Button(self, text="set variable", command=self.x)
                self.button.grid()
       
                self.button1 = Button(self, text="print variable", command=self.y)
                self.button1.grid()
       
app = Application()
app.master.title("test")
app.mainloop()

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

Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.3/lib-tk/Tkinter.py", line 1345, in __call__
return self.func(*args)
File "tmp.py", 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 :)

-cheers-

goestin 01-20-2005 06:52 AM

ah already found it :)

global _x
_x = 6


:rolleyes:

-cheers-


All times are GMT -5. The time now is 02:18 AM.