Search code examples
pythontkinterpython-3.6boolean-logicboolean-operations

What would be the best way to update my boolean using tkinter buttons?


I am working on a calculator and I want it so once the user is done entering the first number and chooses an operator(+-*/ etc.) they can enter a second number and then hit an equals button to get their answer.

Currently, I can not assign a value to my bool inside my defs, which is keeping me from being able to input a second number and eventually perform functions. The last line of commandButtonPress() is where things are slipping up, and prior to making whichNum a list I was getting:

variable referenced before assignment

I have tried both direct assignment as well as using remove() and append(). Buttons 1-9 use the same block of code, and I didn't include the decimal because I didn't think it would be necessary for solving this problem.

I know none of this is elegant and most of it would be far better if I implemented a class, but I am new and trying to figure things out and haven't done object oriented programming in python yet.

from tkinter import *
def buttonOne(whichNum):
    count = 0
    number = ""
    try:
        if whichNum == False and num1[0] != "0":
            num1.append("1")
            while count < len(num1):
                number = number + str(num1[count])
                count += 1
            screen["text"] = number
        elif whichNum == True and num2[0] != "0":
            num2.append("1")
            while count < len(num2):
                number = number + str(num2[count])
                count += 1
            screen["text"] = number
    except:
        if whichNum == False:
            num1[0] = "."
        else:
            num2[0] = "."
        number = "0."
        screen["text"] = number
def commandButtonPress(symbol, whichNum):
    if whichNum == False:
        if len(operator) > 0 and operator[0] == "+":
            operator.remove("+")
    if len(num1) > 0:
        operator.append(symbol)
        whichNum[0] = True
def main():
    window = Tk()
    window.title("Calculator")
    window.geometry("250x305")
    num1 = []
    num2 = []
    operator = []
    whichNum = [False]

    global screen
    screen = Label(text="0", height = 5, width = 30)
    screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])

    button1 = Button(text="1", command = lambda: buttonOne(whichNum[0]))
    button1.grid(column=0, row=2)
    button11 = Button(text="+", command = lambda: commandButtonPress("+", whichNum[0]))
    button11.grid(column=3, row=2)
    window.mainloop()

main()

Solution

  • The solution Nae posted seems to work. My button doesn't handle num2 properly yet, but whichNum is getting updated as it should in commandButtonPress. Thanks for the help. I finished this project (more or less) and uploaded it to my repo.

    from tkinter import *
    whichNum = False
    def buttonOne():
        global whichNum
        count = 0
        number = ""
        try:
            if whichNum == False and num1[0] != "0":
                num1.append("1")
                while count < len(num1):
                    number = number + str(num1[count])
                    count += 1
                screen["text"] = number
            elif whichNum == True and num2[0] != "0":
                num2.append("1")
                while count < len(num2):
                    number = number + str(num2[count])
                    count += 1
                screen["text"] = number
        except:
            if whichNum == False:
                num1[0] = "."
            else:
                num2[0] = "."
            number = "0."
            screen["text"] = number
    def commandButtonPress(symbol):
        global whichNum
        if whichNum == False:
            if len(operator) > 0 and operator[0] == "+":
                operator.remove("+")
        if len(num1) > 0:
            operator.append(symbol)
            whichNum[0] = True
    def main():
        window = Tk()
        window.title("Calculator")
        window.geometry("250x305")
        num1 = []
        num2 = []
        operator = []
        global whichNum
    
        global screen
        screen = Label(text="0", height = 5, width = 30)
        screen.pack(side = LEFT, expand=False, anchor = N, padx=[5, 5])
    
        button1 = Button(text="1", command = lambda: buttonOne())
        button1.grid(column=0, row=2)
        button11 = Button(text="+", command = lambda: commandButtonPress("+"))
        button11.grid(column=3, row=2)
        window.mainloop()
    
    main()