Oct-23-2020, 05:46 PM
(This post was last modified: Oct-23-2020, 05:46 PM by deanhystad.)
Another way to make a much more powerful calculator with much less code is using "eval". The code below has evaluates the equation and displays the result when the user types "=".
from tkinter import * from math import * root = Tk() def evaluate(*args): try: equ = equation.get() if equ[-1] == '=': result.set(str(eval(equ[:-1]))) else: result.set('') except: result.set('') equation = StringVar() equation.trace('w', evaluate) result = StringVar() x = Label(root, text='Equation ') x.grid(row=0, column=0) x = Entry(root, textvar=equation, width=30) x.grid(row=0, column=1) x = Entry(root, textvar=result, width=8, justify=RIGHT, state="readonly") x.grid(row=0, column=2) root.mainloop()To calculate the hypotenuse of a right triangle enter "(3**2 + 4**2)**0.5="