LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 01-20-2005, 06:26 AM   #1
goestin
Member
 
Registered: Nov 2004
Location: Netherlands (east)
Distribution: debian, suse, novell linux desktop
Posts: 81

Rep: Reputation: 15
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-
 
Old 01-20-2005, 06:52 AM   #2
goestin
Member
 
Registered: Nov 2004
Location: Netherlands (east)
Distribution: debian, suse, novell linux desktop
Posts: 81

Original Poster
Rep: Reputation: 15
ah already found it

global _x
_x = 6




-cheers-
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Global Variable? ElementNine Programming 5 11-05-2007 12:53 PM
problems with a global variable javier_ccs Programming 2 06-21-2005 12:18 PM
Defining Global variable in Fedora Kernel tuncausluer Fedora - Installation 0 12-31-2004 01:38 PM
Set Global variable for daemon without login RX100 Linux - Software 3 10-08-2004 08:50 AM
how to define a global variable Anniebaby Programming 1 11-09-2003 10:43 PM


All times are GMT -5. The time now is 04:39 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration