LinuxQuestions.org
Register a domain and help support LQ
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-2004, 10:27 PM   #1
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Rep: Reputation: 15
Python Global Variable?


#!/usr/local/bin/python2.3
from Tkinter import *

status = -1

def changeText() :
status = -status
if status == 1 :
mainLabel['text'] = 'Hello World'
if status == -1 :
mainLabel['text'] = ''


mainWindow = Frame()
mainWindow.pack()

mainLabel = Label(mainWindow)
mainLabel['text'] = 'Hello World'
mainLabel.pack()

mainButton = Button(mainWindow)
mainButton['text'] = 'On/Off'
mainButton['command'] = changeText()
mainButton.pack()

mainWindow.mainloop()


Im trying to learn python, in that script i get an error cuz im trying to access status from changeText how do i make status a global variable to that the script will work?
 
Old 01-20-2004, 11:21 PM   #2
rshaw
Senior Member
 
Registered: Apr 2001
Location: Perry, Iowa
Distribution: Mepis , Debian
Posts: 2,694

Rep: Reputation: 45
to access variables outside the local namespace you need to use the "global" keyword as in:

status = -1

def changetext():
global status
status = -status

if status ==1:
do_stuff
else:
do_other_stuff
 
Old 01-20-2004, 11:27 PM   #3
ElementNine
Member
 
Registered: Sep 2003
Distribution: Red Hat 9 or Gentoo 1.4 whatever I can get to work first
Posts: 105

Original Poster
Rep: Reputation: 15
Well cool that got rid of the errors. but the text doesnt change, is it not possible to modify labels after they have been set ? Guess i should continue reading the book and see if it mentions anything. Thanks though that global keyword helped with alot of other scripts im trying out
 
Old 01-21-2004, 03:13 AM   #4
jimscafe
LQ Newbie
 
Registered: Jul 2003
Posts: 17

Rep: Reputation: 0
I think you need to use the configure method like this

mainLabel.configure(text='New Text')
 
Old 01-21-2004, 07:22 AM   #5
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Quote:
Originally posted by rshaw
to access variables outside the local namespace you need to use the "global" keyword as in:

status = -1

def changetext():
global status
status = -status

if status ==1:
do_stuff
else:
do_other_stuff
Actually, you only need to use the global keyword if you want to CHANGE variables outside the local namespace and you want those changes to stick. Observe:
Code:
>>> foo = 1
>>> def bar():
...     print foo
...
>>> bar()
1
>>> def changefoo():
...     foo = 2
...     print foo
...
>>> changefoo()
2
>>> foo
1
>>> def reallychangefoo():
...     global foo
...     foo = 2
...     print foo
...
>>> reallychangefoo()
2
>>> foo
2
>>>
All functions inherit the set variables of their callers, but only copies of them, not references to them. So if you modify one, it doesn't modify the variable in the caller namespace, just the local namespace ... unless you use global.
 
Old 11-05-2007, 12:53 PM   #6
PerryTachett
LQ Newbie
 
Registered: Nov 2007
Posts: 1

Rep: Reputation: 0
But what about classes?
I have a class, and I want it to get the global variables. How do I do this?

This doesn't seem to work:

Code:
class MyClass:
    global foo, bar
 
  


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
problems with a global variable javier_ccs Programming 2 06-21-2005 12:18 PM
python global variable? goestin Programming 1 01-20-2005 06:52 AM
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 06:16 AM.

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