Paral
Paral

Reputation: 36

Python: Button to reset a label to zero

I am trying to do a count of a price sum, but I am having trouble getting my reset function to make the total price go back to $0. The actual variable that holds the value resets to zero. I just need to reset the label. tkinter is being used.

from tkinter import *
root = Tk()
root.title("QTouch 10")
global total, count
total = 0
count = StringVar()

--List of things I used not involved--

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)

Upvotes: 1

Views: 1331

Answers (1)

cdlane
cdlane

Reputation: 41872

This is my rework to make a minimal example from your code. I removed all the logic having to do with names as it wasn't part of the stated problem and wasn't complete (names wasn't defined.) I've set the initial value of total and count to $10 and let res() reset it to 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()

This appears to work as intended.

Things to note: your explanation said "$0" but your code says "$--"; you don't seem to have a handle on what the global keyword does; you added a layer of indirection via a lambda that may not need to be there.

Upvotes: 1

Related Questions