Tkinter variables Example – Calculator
Tkinter Button Implement a simple calculator describes the design of the simple calculator button layout, and Tkinter eval calculates mathematical expressions describes the use of the eval( ) method, and in this chapter you have learned to use variable categories to control the output of labels. The following section describes the complete calculator design.
Code
Designing a simple calculator. This program uses Lambda extensively in the button design, mainly because the numeric button and the arithmetic expression button use the same function, only the parameters passed are different, so using Lambda can simplify the design.
from tkinter import *
def calculate(): # Perform calculations and display results
result = eval(equ.get())
equ.set(equ.get() + "=\n" + str(result))
def show(buttonString): # Update the calculation formula for the display area
content = equ.get()
if content == "Initialization is complete...":
content = ""
equ.set(content + buttonString)
def backspace(): # Delete the previous character
equ.set(str(equ.get()[:-1]))
def clear(): # Clear the display area and place 0
equ.set("") # equ.set("0")
root = Tk()
root.title("apidemos.com")
# root.geometry("210x223")
equ = StringVar()
equ.set("Initialization is complete...") # The default is to display 0
# Design display area
label = Label(root,width=68,height=3,relief="raised",anchor=SE,
textvariable=equ)
label.grid(row=0,column=0,columnspan=4,padx=5,pady=5)
# Clear display area button
clearButton = Button(root,text="Clear",fg="red",width=15,height=3,command=clear)
clearButton.grid(row = 1,column = 0)
# Here are the other buttons for row1
Button(root,text="Backspace",fg="Green",width=15,height=3,command=backspace).grid(row=1,column=1)
Button(root,text="%",width=15,height=3,command=lambda:show("%")).grid(row=1,column=2)
Button(root,text="/",width=15,height=3,command=lambda:show("/")).grid(row=1,column=3)
# Here are the other buttons for row2
Button(root,text="7",width=15,height=3,command=lambda:show("7")).grid(row=2,column=0)
Button(root,text="8",width=15,height=3,command=lambda:show("8")).grid(row=2,column=1)
Button(root,text="9",width=15,height=3,command=lambda:show("9")).grid(row=2,column=2)
Button(root,text="*",width=15,height=3,command=lambda:show("*")).grid(row=2,column=3)
# Here are the other buttons for row3
Button(root,text="4",width=15,height=3,command=lambda:show("4")).grid(row=3,column=0)
Button(root,text="5",width=15,height=3,command=lambda:show("5")).grid(row=3,column=1)
Button(root,text="6",width=15,height=3,command=lambda:show("6")).grid(row=3,column=2)
Button(root,text="-",width=15,height=3,command=lambda:show("-")).grid(row=3,column=3)
# Here are the other buttons for row4
Button(root,text="1",width=15,height=3,command=lambda:show("1")).grid(row=4,column=0)
Button(root,text="2",width=15,height=3,command=lambda:show("2")).grid(row=4,column=1)
Button(root,text="3",width=15,height=3,command=lambda:show("3")).grid(row=4,column=2)
Button(root,text="+",width=15,height=3,command=lambda:show("+")).grid(row=4,column=3)
# Here are the other buttons for row5
Button(root,text="0",width=32,height=3,
command=lambda:show("0")).grid(row=5,column=0,columnspan=2)
Button(root,text=".",width=15,height=3,
command=lambda:show(".")).grid(row=5,column=2)
Button(root,text="=",width=15,height=3,bg="yellow",
command=lambda:calculate()).grid(row=5,column=3)
root.mainloop()
Output:
Calculate the result of 5+9*6
:
Click on Cleaer
results: