簡體   English   中英

Python Tkinter Textvariable不更新

[英]Python tkinter textvariable not updating

我有一個經常更改的標簽,因此已將其放在函數中。
但是當我調用該函數時,不會顯示標簽。 如果我將textvarible設置為text,則它可以正常工作。
我究竟做錯了什么?

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))

不必每次調用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))

然后從updateSearchLabel內部調用set

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM