It's an extremely simple Python Tk program. I can't seem to stop a simple problem and I am sure I am missing something easy.
I make a label:
myLabelText = StringVar()
myLabelText.set("Something kinda long")
myLabel = Label(frame, textvariable=myLabelText).pack()
Later in the same program I want to update that label to say "Foo"...
myLabelText.set("Foo")
frame.update_idletasks()
The label now looks like "Fooething kinda long" The goal would be to just have "Foo" and clear the rest of the label text.
I tried to set the label to a long string of spaces but for some reason that's not clearing the text in that field. What is the right way to do this?
Edit
Here is a complete example that demonstrates my problem.
from Tkinter import *
import tkFileDialog, Tkconstants
import time
import urllib2
def main():
" Controlling function "
root = Tk()
app = App(root)
root.mainloop()
class App:
" Class for this application "
def __init__(self, master):
# Setup the window
frame = Frame(master, width=400, height=200)
frame.pack()
frame.pack_propagate(0)
self.frame = frame
self.master = master
# Start Button
self.button = Button(frame, text='Start', bg="#339933", height=3, width=10, command=self.start)
self.button.pack()
# Label
self.operation_action_text = StringVar()
self.operation_action_text.set("Waiting on user to click start...")
self.operation_action = Label(frame, textvariable=self.operation_action_text)
self.operation_action.pack()
def start(self):
" Change the label "
# Do something and tell the user
response = urllib2.urlopen('http://www.kennypyatt.com')
json_string = response.read()
self.operation_action_text.set("Something kinda long")
self.frame.update_idletasks()
time.sleep(2)
# Do something else and tell the user
response = urllib2.urlopen('http://www.kennypyatt.com')
json_string = response.read()
self.operation_action_text.set("ABCDEFGHI")
self.frame.update_idletasks()
time.sleep(2)
# Do a third thing and tell the user
response = urllib2.urlopen('http://www.kennypyatt.com')
json_string = response.read()
self.operation_action_text.set("FOO")
self.frame.update_idletasks()
return
main()