[Python] Python Tkinter Calculator wont evaluate text from entry widget

Discussão em 'Python' iniciado por Stack, Junho 13, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to make a simple calculator app using tkinter, but everytime I run the code below i get an error message saying

    Traceback (most recent call last):
    File "C:\Python33\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 326, in RunScript
    exec(codeObject, __main__.__dict__)
    File "C:\Users\csp\Python\Calculator App.py", line 17, in <module>
    solved = eval(expression)
    File "<string>", line 0

    ^
    SyntaxError: unexpected EOF while parsing


    CODE:

    from tkinter import *
    tk = Tk()
    tk.title('Calculator')
    inp = Entry(tk,text="Enter Expression Here",width=20)
    inp.pack()
    exit = False
    def exitbtn():
    global exit
    exit = True
    return exit
    btn = Button(tk,text="Quit?",command=exitbtn)
    btn.pack
    canvas = Canvas(tk,width=200,height=200)
    canvas.pack()
    while not exit:
    expression = inp.get()
    solved = eval(expression)
    canvas.create_text(100,100,text=expression,font=('Times', 15))
    canvas.create_text(100,150,text=solved,font=('Times', 15))
    if exit == True:
    break
    tk.destroy()


    i am really new to Python and dont understand why the "solved = eval(expression)" line wont work. please help

    Continue reading...

Compartilhe esta Página