Calculator app works fine but after one operation the result is always 0

from customtkinter import *

import tkinter.messagebox

app = CTk()

app.geometry("420x500")

app.resizable(False, False)

set_appearance_mode("dark")

tempLabel = ""

valuesInt = ""

valuesInt2 = ""

i = 0

i2 = 0

symbol = ""

#[FUNCTIONS]

def labelUpdate(button):

    global tempLabel

    tempLabel += button.cget("text")

    resultLabel.configure(text=tempLabel)

def clear():

    global valuesInt

    global valuesInt2

    global tempLabel

    global i

    global i2

    resultLabel.configure(text="")

    tempLabel = ""

    valuesInt = ""

    valuesInt2 = ""

    i = 0

    i2 = 0



def result():

    global i

    global i2

    global tempLabel

    global symbol

    global valuesInt

    global valuesInt2

    resultNumber = 0

    if symbol == "+":

                resultNumber = valuesInt + int(valuesInt2)

    elif symbol == "-":

                resultNumber = valuesInt - int(valuesInt2)

    elif symbol == "x":

                resultNumber = valuesInt * int(valuesInt2)

    elif symbol == "÷":

                resultNumber = valuesInt / int(valuesInt2)

    else:

          resultNumber = resultNumber

    resultLabel.configure(text=resultNumber)

    print(resultNumber)

    tempLabel = ""

    valuesInt = ""

    valuesInt2 = ""

    i = 0

    i2 = 0

def valueAppend(button):

    global i2

    global i

    global tempLabel

    global symbol

    global valuesInt

    global valuesInt2

   

    try:

        intText = int(button.cget("text"))

        if i2 == 0:

            valuesInt += str(intText)

        elif i2 != 0:

            valuesInt = int(valuesInt)

            valuesInt2 += str(intText)

    except:

        i2 += 1

        if i == 0:

            symbol += button.cget("text")

            i += 1

        elif i >= 1:

            tempLabel = tempLabel[:-1]

            resultLabel.configure(text=tempLabel)

            tkinter.messagebox.showwarning("error", "you can only put one symbol per operation")

       

#[BUTTONS, LABELS ETC.]

resultLabel = CTkLabel(master=app, text="...", font=("Arial", 50))

resultLabel.place(x=80, y=30)

number1 = CTkButton(master=app, text="1", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number1), valueAppend(number1)])

number1.place(x=10, y=130)

number2 = CTkButton(master=app, text="2", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number2), valueAppend(number2)])

number2.place(x=115, y=130)

number3 = CTkButton(master=app, text="3", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number3), valueAppend(number3)])

number3.place(x=220, y=130)

number4 = CTkButton(master=app, text="4", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number4), valueAppend(number4)])

number4.place(x=10, y=230)

number5 = CTkButton(master=app, text="5", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number5), valueAppend(number5)])

number5.place(x=115, y=230)

number6 = CTkButton(master=app, text="6", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number6), valueAppend(number6)])

number6.place(x=220, y=230)

number7 = CTkButton(master=app, text="7", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number7), valueAppend(number7)])

number7.place(x=10, y=330)

number8 = CTkButton(master=app, text="8", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number8), valueAppend(number8)])

number8.place(x=115, y=330)

number9 = CTkButton(master=app, text="9", font=("Arial", 25), height=90, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=5, command=lambda: [labelUpdate(number9), valueAppend(number9)])

number9.place(x=220, y=330)

#---------------------------------------

clearButton = CTkButton(master=app, text="Clear", font=("Arial", 20), height=65, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=clear)

clearButton.place(x=10, y= 430)

equalsButton = CTkButton(master=app, text="=", font=("Arial", 20), height=65, width=100, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=result)

equalsButton.place(y=430, x=130)

plusButton = CTkButton(master=app, text="+", font=("Arial", 20), height=100, width=65, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=lambda: [labelUpdate(plusButton), valueAppend(plusButton)])

plusButton.place(y=390, x=335)

minusButton = CTkButton(master=app, text="-", font=("Arial", 20), height=100, width=65, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=lambda: [labelUpdate(minusButton), valueAppend(minusButton)])

minusButton.place(y=285, x=340)

multiplyButton = CTkButton(master=app, text="x", font=("Arial", 20), height=100, width=65, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=lambda: [labelUpdate(multiplyButton), valueAppend(multiplyButton)])

multiplyButton.place(y=180, x=340)

divButton = CTkButton(master=app, text="÷", font=("Arial", 20), height=100, width=65, bg_color="transparent",

                     fg_color="transparent", corner_radius=32, border_color="#511880", border_width=3, command=lambda: [labelUpdate(divButton), valueAppend(divButton)])

divButton.place(y=75, x=340)

app.mainloop()

Please Tell us what’s happening in your own words.

Learning to describe problems is hard, but it is an important part of learning how to code.

Also, the more you say, the more we can help!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.