-1

I have been trying to use the tkinter module for creating a user interface for a project I've been working on. For some reason, the command doesn't do what i'm asking from it and the price stays as 0 on the main code.

total = 0

def item1(total):
    currentOrder.append('All day (large)')
    print(currentOrder)
    price = 5.50
    total = total + price
    print(total)
    return total

def menu():
    root = tkinter.Tk()

    root.minsize(width=800, height=428)
    root.maxsize(width=800, height=428)



    i1 = tkinter.Button(root, text = 'Item 1', command=item1(total))
    i1.place(x=80, y=30 ,width=120 ,height=26,  anchor = 's')


menu()

2 Answers2

1

You can define total as global within item1 method:

import tkinter

PRICE = 5.50
total = 0


def item1():
    global total
    total += PRICE
    print(total)


def menu():
    root = tkinter.Tk()

    root.minsize(width=800, height=428)
    root.maxsize(width=800, height=428)

    btn = tkinter.Button(root, text='Item 1', command=item1)
    btn.place(x=80, y=30, width=120, height=26, anchor='s')

    root.mainloop()


menu()

Output:

5.5
11.0
16.5
22.0
Alderven
  • 7,569
  • 5
  • 26
  • 38
0

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:

  1. 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)
  1. 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()
Lapis Rose
  • 644
  • 3
  • 15