无法获取python从属变量以更新con

2024-09-28 03:17:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我创建一个StringVar,将它从属于一个标签,更改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

Tags: thetextselffalsedefstatus标签width
1条回答
网友
1楼 · 发布于 2024-09-28 03:17:43

我把你的代码改写成了有用的东西。窗口打开时显示文本“Initial”,然后每隔一秒在“Welcome 1”和“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')

这个问题非常类似于Update Tkinter Label from variable

相关问题 更多 >

    热门问题