简体   繁体   English

Python Tkinter Textvariable不更新

[英]Python tkinter textvariable not updating

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. 如果我将textvarible设置为text,则它可以正常工作。
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))

Instead of making a new label each time updateSearchLabel is called, make the Label once, and retain a reference to the StringVar : 不必每次调用updateSearchLabel都创建一个新标签,而是创建一次Label ,并保留对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 : 然后从updateSearchLabel内部调用set

def updateSearchLabel(self, textVar):
    self.lblSearchText.set(textVar)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM