我正在尝试计算价格总和,但我无法使用重置功能将总价格恢复为 0 美元。保存该值的实际变量重置为零。我只需要重置标签。正在使用 tkinter。
from tkinter import *
root = Tk()
root.title("QTouch 10")
global total, count
total = 0
count = StringVar()
--我用过的不涉及的东西列表--
amount = Label(root, textvariable=count)
count.set("$--")
def res():
total = 0
**count.set("$--")**
for n in names:
names[names.index(n)][3] = 0
label = Label(root, text="0").grid(row=names.index(n)+1, column=4)
amount.grid(row=10, column=4)
reset = Button(root, text="Reset", command=lambda *args: res()).grid(row=10,
column=1)
最佳答案
这是我的返工,从您的代码中制作一个最小的示例。我删除了与 names
有关的所有逻辑,因为它不是所述问题的一部分并且不完整(names
未定义。)将 total
和 count
的初始值设置为 $10,并让 res()
将其重置为 0:
from tkinter import *
root = Tk()
total = 10
count = StringVar()
amount = Label(root, textvariable=count)
count.set("$10")
def res():
global total
total = 0
count.set("$0")
amount.grid(row=10, column=4)
Button(root, text="Reset", command=res).grid(row=10, column=1)
root.mainloop()
这似乎按预期工作。
需要注意的事项:你的解释是“$0”
,但你的代码是“$--”
;您似乎不了解 global
关键字的作用;您通过 lambda
添加了一个可能不需要的间接层。
关于Python:将标签重置为零的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49642968/