首页 > 解决方案 > 为什么 python 给出这个程序的 self.var 的垃圾值?

问题描述

我是 python 新手。我创建了一个面向对象的 GUI,它显示了一个简单的状态栏。当我们按下一个按钮时,状态将改变(准备上传)2秒。但是python给出了一个像 PY_VAR0 这样的垃圾值。并且状态不会改变。我应该怎么做才能解决这个错误?

from tkinter import *



class Status(Tk):


    def __init__(self):
        super().__init__()

        self.geometry('750x550')
        self.title('OOP With Py')
        


    def varValue(self):
        self.var=StringVar()
        self.var.set('Ready Now')

        return self.var


    def upload(self):
        import time
        self.var.set('Uploading')
        self.l.update()
        time.sleep(2)
        self.var.set('Ready')

    


    def Label(self):
        self.l=Label(self,text=self.varValue(),anchor=W,relief=SUNKEN,font='lucida 20 bold')
        self.l.pack(side=BOTTOM,fill=X)


    def button(self,text):
        Button(self,text=text,command=self.upload).pack()





if __name__=='__main__':
    window=Status()
    window.Label()
    window.varValue()
    # window.upload()
    window.button('Upload')
    window.mainloop()

标签: pythonuser-interfacetkinter

解决方案


您需要使用文本变量,而不是文本

def Label(self):
    self.l=Label(self,textvariable=self.varValue(),anchor=W,relief=SUNKEN,font='lucida 20 bold')
    self.l.pack(side=BOTTOM,fill=X)

推荐阅读