How to change Tkinter Label widget text without global variable in a class?

Thomas Hoffend trhoffend at
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 *
import os
import os.path

path = os.path.abspath('.')
fullpath = os.path.normpath(path)

class mywidget:
    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:
    global vCurrentPath
    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):
    def go_up_one(self):
        print "Going Up!"
        self.PathStr = (os.path.dirname(self.PathStr))
        print self.PathStr

root = Tk()
app = mywidget(root)

More information about the Python-list mailing list