python - 为什么 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()
解决方案
您需要使用文本变量,而不是文本
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)
推荐阅读
- mocha.js - package.json 中的通配符故障
- javascript - 如何使用 getByTestId 选择元素,该元素也具有 react-testing-library 的特定值
- github - 来自集群节点的 ssh 触发所有远程主机的公钥错误(MWE for github)
- reactjs - React/NextJS 使用 UseEffect 错误:渲染的钩子比上一次渲染期间更多
- mysql - MYSQL查询以显示按内部连接列分组的列的最大值
- node.js - 加载资源失败:服务器在 MERN 中响应状态为 422(无法处理的实体)
- python-3.x - 使三个数据帧的最大值索引相似
- html - 如何在嵌套 SVG 中设置“preserveAspectRatio”属性?
- google-apps-activity - 如何在 Google Drive Activity API 中过滤掉 MIMETYPE
- discord - discord.py bot 检测用户名更改