0

This code is working fine to develop the GUI but whenever I click a button on GUI, nothing happens, no changes occur to textbox. I checked if the functions are being called by using print statements and they were.



import tkinter

calculation = ""





def add(value):

    global calculation

    calculation += str(value)

    text_result.delete(1.0, "end")





def evaluate():

    global calculation

    try:

        calculation = str(eval(calculation))

        text_result.delete(1.0, "end")

        text_result.insert(1.0, calculation)

    except:

        clear()

        text_result.insert(1.0, "ERROR")



def clear():

    global calculation

    calculation = ""

    text_result.delete(1.0, "end")





root = tkinter.Tk()

root.geometry("350x200")

root.title("Simple Calculator")



text_result = tkinter.Text(root, height=2, width=16, font=("Arial", 26))

text_result.grid(row=0, column=0, columnspan=5)

btn1 = tkinter.Button(root, text="1", command=lambda: add(1), width=5, font=("Arial", 14))

btn2 = tkinter.Button(root, text="2", command=lambda: add(2), width=5, font=("Arial", 14))

btn3 = tkinter.Button(root, text="3", command=lambda: add(3), width=5, font=("Arial", 14))

btn4 = tkinter.Button(root, text="4", command=lambda: add(4), width=5, font=("Arial", 14))

btn5 = tkinter.Button(root, text="5", command=lambda: add(5), width=5, font=("Arial", 14))

btn6 = tkinter.Button(root, text="6", command=lambda: add(6), width=5, font=("Arial", 14))

btn7 = tkinter.Button(root, text="7", command=lambda: add(7), width=5, font=("Arial", 14))

btn8 = tkinter.Button(root, text="8", command=lambda: add(8), width=5, font=("Arial", 14))

btn9 = tkinter.Button(root, text="9", command=lambda: add(9), width=5, font=("Arial", 14))

btn0 = tkinter.Button(root, text="0", command=lambda: add(0), width=5, font=("Arial", 14))

btn_plus = tkinter.Button(root, text="+", command=lambda: add("+"), width=10, font=("Arial", 20))

btn_minus = tkinter.Button(root, text="-", command=lambda: add("-"), width=10, font=("Arial", 20))

btn_mul = tkinter.Button(root, text="x", command=lambda: add("*"), width=10, font=("Arial", 20))

btn_div = tkinter.Button(root, text="/", command=lambda: add("/"), width=10, font=("Arial", 20))

btn_clear = tkinter.Button(root, text="C", command=clear, width=5, font=("Arial", 14))

btn_eql = tkinter.Button(root, text="=", command=evaluate, width=5, font=("Arial", 14))

btn1.grid(row=2, column=1)

btn2.grid(row=2, column=2)

btn3.grid(row=2, column=3)

btn4.grid(row=3, column=1)

btn5.grid(row=3, column=2)

btn6.grid(row=3, column=3)

btn7.grid(row=4, column=1)

btn8.grid(row=4, column=2)

btn9.grid(row=4, column=3)

btn0.grid(row=5, column=2)

btn_plus.grid(row=2, column=4)

btn_minus.grid(row=3, column=4)

btn_mul.grid(row=4, column=4)

btn_div.grid(row=5, column=4)

btn_clear.grid(row=5, column=1)

btn_eql.grid(row=5, column=3)



root.mainloop()
4
  • I have to ask one thing, Do you want to clear the result after one calculation is complete? I mean let's say the user did 2+2, then the result will be 4 in the text box, and for example, after that user presses + and 1, now you want 4+1 or just +1 Commented Jul 16, 2024 at 11:49
  • The main problem is in add function you are clearing the result and not adding anything Commented Jul 16, 2024 at 11:49
  • No I don't want to clear result after every calculation. The user should be able do further calculation on previous one. Commented Jul 16, 2024 at 12:24
  • I did add a statement for insertion in add function but it's not working. text_result.insert(1.0, calculation) Commented Jul 16, 2024 at 12:25

3 Answers 3

1

Try to change this - worked for me:

def add(value):
    global calculation
    calculation += str(value)
    text_result.insert("end", str(value))
Sign up to request clarification or add additional context in comments.

3 Comments

Tried it. Didn't work.
@MinahilKhawaja this should work, as it is working on my side
Does this has to do anything with the system and IDE? I'm using macbook and pycharm
1

The problem is that you aren't inserting the text for the button. Your add function should clear the old value and put in the new value of calculation.

For example:

def add(value):
    global calculation

    calculation += str(value)

    text_result.delete(1.0, "end")
    text_result.insert("1.0", calculation)

Another option would be to not delete anything, and just add the new value to the end so that calculation and the text widget stay in sync (ie: when you change calculation make the same change to the text widget).

def add(value):
    global calculation

    calculation += str(value)

    text_result.insert("end", value)

Also, when you call a text widget function with an index (such as text_result.insert(1.0, ...)), the index should be a string rather than a floating point number (eg: text_result.insert("1.0", ...)).

Comments

1

Textbox in calculator not updating when button clicked

but whenever I click a button on GUI, nothing happens, no changes occur to textbox. I checked if the functions are being called by using print statements and they were.

The problem can be fixed.

After applying from @Bryan Oakley.

  • Your Button widget grid(row= layout isn't accurate.
  • Your equal's button isn't updating the answer.

Snippet:

root.geometry("350x300")#add 300

def evaluate():
    global calculation
    result = str(eval(calculation))
    calculation = ""
    text_result.insert('end', f'= {result}')


btn1.grid(row=1, column=1)

btn2.grid(row=1, column=2)

btn3.grid(row=1, column=3)

btn4.grid(row=2, column=1)

btn5.grid(row=2, column=2)

btn6.grid(row=2, column=3)

btn7.grid(row=3, column=1)

btn8.grid(row=3, column=2)

btn9.grid(row=3, column=3)

btn0.grid(row=4, column=2)

btn_plus.grid(row=1, column=4)

btn_minus.grid(row=2, column=4)

btn_mul.grid(row=3, column=4)

btn_div.grid(row=4, column=4)

btn_clear.grid(row=4, column=1)

btn_eql.grid(row=4, column=3)grid

Screenshot:

enter image description here

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.