Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Non-*NIX Forums > Programming
User Name
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.


  Search this Thread
Old 01-20-2005, 06:26 AM   #1
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..
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

Old 01-20-2005, 06:52 AM   #2
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



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 > Forums > Non-*NIX Forums > Programming

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

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