为什么python给这个程序的self.var垃圾值?
我是python新手。我创建了一个面向对象的GUI,它显示了一个简单的状态栏。当我们按下一个按钮时,状态将改变(准备上传)2秒。但是python给出了一个类似PY_VAR0的垃圾值。而且状态没有改变。我应该如何解决此错误为什么python给这个程序的self.var垃圾值?,python,user-interface,tkinter,Python,User Interface,Tkinter,我是python新手。我创建了一个面向对象的GUI,它显示了一个简单的状态栏。当我们按下一个按钮时,状态将改变(准备上传)2秒。但是python给出了一个类似PY_VAR0的垃圾值。而且状态没有改变。我应该如何解决此错误 from tkinter import * class Status(Tk): def __init__(self): super().__init__() self.geometry('750x550') s
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()
您需要使用文本变量,而不是文本
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)
首先,您应该使用
textvariable
选项,而不是text
:
def标签(自身):
self.l=Label(self,textvariable=self.varValue(),anchor=W,relief=sinken,font='lucida 20 bold')
自包装(侧面=底部,填充=X)
其次,您不应该在window.Label()
之后调用window.varValue()
,因为它将StringVar()
的新实例分配给self.var
。因此,更新self.var
不会更新self.l
:
if __name__=='__main__':
window=Status()
window.Label()
#window.varValue() ### should not be called
# window.upload()
window.button('Upload')
window.mainloop()
非常感谢。但为什么地位没有改变?