I created a tkinter python tally app and i just want to be able to plus 1 or minus 1 from the click of a button. Just a regular tally app. The problem im having is that the plus 1 always works but when i want to minus 1 it automatically turns to a negative number. here is my script:
total_count = 0
def count_number():
global total_count
total_count = total_count 1
count_label["text"] = total_count
minus_count = total_count
def minus_number():
global minus_count
minus_count = minus_count - 1
count_label["text"] = minus_count
count_label = Label(root, fg="orange", font="Verdana 150 bold italic")
count_label.pack()
count_label.place(x=810, y=280)
plus_button = Button(root, text=" 1", width=10, fg="purple", command=count_number).place(x=910, y=870)
minus_button = Button(root, text="- 1", width=10, fg="purple", command=minus_number).place(x=910, y=970)
How can i both add and subtract 1 from the same number?
CodePudding user response:
At the line minus_count = total_count
minus_count
is assigned the value of total_count
which is 0. So these events occur in order:
total_count = 0
minus_count = 0
- plus is executed, which increases
total_count
, notminus_count
- minus is executed which subtracts 1 from
minus_count
, resulting in-1
Easy fix will be to use the same variable in both places:
total_count = 0
def count_number():
global total_count
total_count = total_count 1
count_label["text"] = total_count
def minus_number():
global total_count
total_count= total_count- 1
count_label["text"] = total_count
count_label = Label(root, fg="orange", font="Verdana 150 bold italic")
count_label.pack()
count_label.place(x=810, y=280)
plus_button = Button(root, text=" 1", width=10, fg="purple", command=count_number).place(x=910, y=870)
minus_button = Button(root, text="- 1", width=10, fg="purple", command=minus_number).place(x=910, y=970)