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? |
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 |
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
|
I think you need to use the configure method like this
mainLabel.configure(text='New Text') |
Quote:
Code:
>>> foo = 1 |
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: |
All times are GMT -5. The time now is 10:31 AM. |