How to change Tkinter Label widget text without global variable in a class?
trhoffend at mediaone.net
Thu Jul 19 15:43:21 CEST 2001
I am new to Python and working on learning Tkinter.
I ordered some of the books recommended on the Python web site, and also
have been going
through the online tutorial on Tkinter and reading old newgroup articles.
There is one point which
I found that sort of bugs me, and that is I can't figure out how to change a
Label widget text
without using some sort of global variable. It seems that declaring a
global variable inside a class
is not the best thing to do, especially if there is the possibility that you
will create multiple instances of
the class (unless maybe I don't understand the scope of the global
variable - is it
global just inside the class?). So is there some way to change the label
text without a global
variable? For example:
# Script to pop up the current directory name in a window and go up one
# I hope to turn this into a directory selector.
from Tkinter import *
path = os.path.abspath('.')
fullpath = os.path.normpath(path)
def __init__(self, master):
frame = Frame(master)
# Make a button to go up one directory (well, at least manipulate a
string with the path name for now):
self.upbutton = Button(frame, text="Up", command=self.go_up_one)
# Put some text under the button to show the path:
vCurrentPath = StringVar()
label1 = Label(frame, textvariable=vCurrentPath)
# The class will store the full path:
PathStr = fullpath
# Here is a function to go up one directory - eventually (right now it
just manipulates the path name):
print "Going Up!"
self.PathStr = (os.path.dirname(self.PathStr))
root = Tk()
app = mywidget(root)
More information about the Python-list