This is because you never update total. Tkinter buttons can take a function for the command parameter and use it as a callback. This function is called when the button is pressed and the function doesn't have anywhere to return an output to. Because of this, any changes that happen to "total" is lost and the outer scope of your program is oblivious to these changes. At the end of your button press, "total" is still 5.5.
In order to make your button able to change the variable "total", you must change your code to allow the method to reference the variable and mutate it without losing the reference after the function ends.
You can do this multiple ways:
- Make "total" a global variable in the scope of your item1 method. Though global variables may be heavily frowned upon, they allow for mutability within methods and are a simple way to fix your problem.
total = 0
def item1():
global total
currentOrder.append('All day (large)')
print(currentOrder)
price = 5.50
total = total + price
print(total)
- Place your variables in a class so that your methods can reference them within the scope of the class instead of globally. This is a little more elaborate but will allow you to avoid creating global variables.
class tkinter_module:
def __init__(self):
self.total = 0
def item1(self):
currentOrder.append('All day (large)')
print(currentOrder)
price = 5.50
self.total = self.total + price
print(self.total)
def menu(self):
root = tkinter.Tk()
root.minsize(width=800, height=428)
root.maxsize(width=800, height=428)
i1 = tkinter.Button(root, text = 'Item 1', command = self.item1())
i1.place(x=80, y=30 ,width=120 ,height=26, anchor = 's')
root.mainloop()
tk_module = tkinter_module()
tk_module.menu()