import tkinter as tk
import tkinter.messagebox as mgx
def command_1():
global string
string += "1"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_2():
global string
string += "2"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_3():
global string
string += "3"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_4():
global string
string += "4"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_5():
global string
string += "5"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_6():
global string
string += "6"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_7():
global string
string += "7"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_8():
global string
string += "8"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_9():
global string
string += "9"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_0():
global string
string += "0"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_ce():
global string
string = ""
entry.delete(0, tk.END)
entry.insert(0, string)
def command_del():
global string
string = string[:-1]
entry.delete(0, tk.END)
entry.insert(0, string)
def command_add():
global string
string += "+"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_jian():
global string
string += "-"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_cheng():
global string
string += "*"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_chu():
global string
string += "/"
entry.delete(0, tk.END)
entry.insert(0, string)
def command_deng():
global string
try:
result = eval(string)
entry.delete(0, tk.END)
entry.insert(0, str(result))
string = str(result)
except:
mgx.showerror("Error", "你输入的不是表达式!")
string = ""
def command_mi():
global string
string += "**"
entry.delete(0, tk.END)
entry.insert(0, string)
string = ""
root = tk.Tk()
root.title("Calculator")
root.geometry("300x450")
entry = tk.Entry(root, width=40)
entry.pack()
button_1 = tk.Button(root, text="1", command=command_1)
button_1.place(x=7, y=35, width=60, height=60)
button_2 = tk.Button(root, text="2", command=command_2)
button_2.place(x=107, y=35, width=60, height=60)
button_3 = tk.Button(root, text="3", command=command_3)
button_3.place(x=207, y=35, width=60, height=60)
button_4 = tk.Button(root, text="4", command=command_4)
button_4.place(x=7, y=105, width=60, height=60)
button_5 = tk.Button(root, text="5", command=command_5)
button_5.place(x=107, y=105, width=60, height=60)
button_6 = tk.Button(root, text="6", command=command_6)
button_6.place(x=207, y=105, width=60, height=60)
button_7 = tk.Button(root, text="7", command=command_7)
button_7.place(x=7, y=175, width=60, height=60)
button_8 = tk.Button(root, text="8", command=command_8)
button_8.place(x=107, y=175, width=60, height=60)
button_9 = tk.Button(root, text="9", command=command_9)
button_9.place(x=207, y=175, width=60, height=60)
button_0 = tk.Button(root, text="0", command=command_0)
button_0.place(x=7, y=245, width=60, height=60)
button_ce = tk.Button(root, text="CE", command=command_ce)
button_ce.place(x=107, y=245, width=60, height=60)
button_del = tk.Button(root, text="del", command=command_del)
button_del.place(x=207, y=245, width=60, height=60)
button_add = tk.Button(root, text="+", command=command_add)
button_add.place(x=7, y=315, width=60, height=60)
button_jian = tk.Button(root, text="-", command=command_jian)
button_jian.place(x=107, y=315, width=60, height=60)
button_cheng = tk.Button(root, text="x", command=command_cheng)
button_cheng.place(x=207, y=315, width=60, height=60)
button_chu = tk.Button(root, text="÷", command=command_chu)
button_chu.place(x=7, y=385, width=60, height=60)
button_deng = tk.Button(root, text="=", command=command_deng)
button_deng.place(x=107, y=385, width=60, height=60)
button_mi = tk.Button(root, text="^", command=command_mi)
button_mi.place(x=207, y=385, width=60, height=60)
root.mainloop()