0

I create a StringVar, slave it to a label, change the value of the StringVar, and the label does not update.

class DlgConnection:

    def start(self)
        self.dlgConnection = Tk()
        self.dlgConnection.title("Connection")
        self.dlgConnection.geometry("380x400")
        self.dlgConnection.resizable(width=False,height=False)
        self.statusText = StringVar()
        self.lStatusText = Label(self.dlgConnection, width=80, anchor="w", textvariable=self.statusText)
        self.lStatusText.place(x = 0, y = 360, width=380, height=25)
        self.setStatus("Welcome 2")

    def setStatus(self, status_text):
        print(status_text) #the print is just to show me I changed the text
        self.statusText.set(status_text)
        self.lStatusText.update() # I tried this out of desperation
9000
  • 39,899
  • 9
  • 66
  • 104
  • Works for me, filling in the blanks in your incomplete code in a reasonable way; the problem is likely in the code you didn't post. – jasonharper Jan 19 '17 at 16:48
  • 3
    In your real program do you do `Tk()` in more than one place, or is this function literally the only place where you create the root window? – Bryan Oakley Jan 19 '17 at 16:49

1 Answers1

0

I rewrote your code to something that works. The window opens with the text "Initial" and then once a second switches between "Welcome 1" and "Welcome 2".

from tkinter import *
from time import sleep

class DlgConnection(Frame):

    def __init__(self, master):
        Frame.__init__(self, master)

        master.title("Connection")
        master.geometry("380x400")
        master.resizable(width=False,height=False)
        self.statusText = StringVar()
        self.statusText.set('Initial')
        self.lStatusText = Label(master, width=80, anchor="w", textvariable=self.statusText)
        self.lStatusText.place(x = 0, y = 360, width=380, height=25)

        self.pack()

    def setStatus(self, status_text):
        print(status_text) #the print is just to show me I changed the text
        self.statusText.set(status_text)
        self.update_idletasks()

root = Tk()
dlgConnection = DlgConnection(root)
dlgConnection.update()

for i in range(6):
    sleep(1) # Need this to slow the changes down
    dlgConnection.setStatus('Welcome 2' if i%2 else 'Welcome 1')

This question is very similar to Update Tkinter Label from variable