无法获取python从属变量以更新控件
我创建一个StringVar,将其从属于一个标签,更改无法获取python从属变量以更新控件,python,tkinter,Python,Tkinter,我创建一个StringVar,将其从属于一个标签,更改StringVar的值,标签不会更新 class DlgConnection: def start(self) self.dlgConnection = Tk() self.dlgConnection.title("Connection") self.dlgConnection.geometry("380x400") self.dlgConnection.resizab
StringVar
的值,标签不会更新
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
我把你的代码改写成了可以用的东西。窗口打开时显示文本“初始”,然后每秒在“欢迎1”和“欢迎2”之间切换一次
这个问题非常类似于对我有效,以合理的方式填补不完整代码中的空白;问题可能出在您没有发布的代码中。在您的实际程序中,您是在多个位置执行
Tk()
,还是该函数实际上是创建根窗口的唯一位置?我正在Windows上运行Python 3.4(如果这很重要的话)。
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')