Tkinter eval calculates mathematical expressions
Python has a very nice function within eval that computes mathematical expressions and passes back the result of this mathematical expression directly.
The syntax format of eval is as follows:
result = eval(expression) # expression is a string
The result of the above calculation is also passed back as a string.
Tkinter eval calculates mathematical expressions Example#1
Enter the mathematical expression and this program will pass back the execution result.
from tkinter import *
# expression = input("Please enter the mathematical expression :")
expression = "9*8+5+10"
print("Result:",eval(expression))
Output:
After understanding the usage of the eval() function, the above program can be changed to a GUI design.
Tkinter eval calculates mathematical expressions Example#1
Enter a mathematical expression within Entry and this program will list the results.
from tkinter import *
def cal():
out.configure(text = "Result:" + str(eval(equ.get())))
# equ.delete(0,END) # Clear the input box when the calculation is complete
root = Tk()
root.title("apidemos.com")
label = Label(root,text="Please enter the mathematical expression :")
label.pack()
equ = Entry(root)
equ.pack(pady=5)
out = Label(root)
out.pack()
btn = Button(root,text="Calculate",command=cal)
btn.pack(pady=5)
root.mainloop()
Output: