Question

I have a label that changes often, so I've put it in a function.
But when I call the function, the label is not displayed. If I set the textvarible to text, then it's working as expected.
What am I doing wrong?

text = "Now visible to others as {}".format(SERVER_NAME)
        self.updateSearchLabel(text)

def updateSearchLabel(self, textVar):
    text = StringVar()
    text.set(textVar)
    self.lblSearch = Label(self.gpBt, textvariable=text)        
    self.lblSearch.grid(row=0, column=0, sticky=W, padx=(10,0), pady=(5,0))
Was it helpful?

Solution

Instead of making a new label each time updateSearchLabel is called, make the Label once, and retain a reference to the StringVar:

def createSearchLabel(self):
    self.lblSearchText = StringVar()
    self.lblSearch = Label(self.gpBt, textvariable=self.lblSearchText)        
    self.lblSearch.grid(row=0, column=0, sticky=W, padx=(10,0), pady=(5,0))

and then call set from within updateSearchLabel:

def updateSearchLabel(self, textVar):
    self.lblSearchText.set(textVar)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top