Python:将标签重置为零的按钮

标签 python tkinter label

我正在尝试计算价格总和,但我无法使用重置功能将总价格恢复为 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 未定义。)将 totalcount 的初始值设置为 $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/

相关文章:

python - 将字典列表转换为字典

python - 使用 csv 文件中的 matplotlib 在 python 中绘制时间序列图

python - 如何每60秒刷新一次python tkinter窗口的内容

python - PyQt5 - 调整标签大小以填充整个窗口

python - 使用字符串搜索 Pandas 系列会产生 KeyError

c++ - 将静态库链接到 Boost Python(共享库)- 导入错误

python tkinter 按钮事件

python - 如何在同一框架中使用按钮显示命令addBox

php - 如何使用 PhpSpreadSheet 折线图在每个标记中设置可见数据标签?

css - Mapbox studio labeling - 按特征大小优先放置标签