import tkinter as tk
p = tk.Tk()
p.geometry("280x500")
p.title("德钦计算器")
label1 = tk.Label(
p,
font=('Arial', 20),
bg="#8A2BE2",
fg="#000000",
bd=9,
anchor='se',
text="计算结果"
)
label1.place(x=0, y=0, width=280, height=170)
label2 = tk.Label(
p,
font=('Arial', 20),
bg="#8A2BF5",
fg="#000000",
bd=9,
anchor='se',
text="计算公式"
)
label2.place(x=0, y=170, width=280, height=60)
def btn(t, x, y, color="#0000FF",width = 70,height = 55):
btn7 = tk.Button(
p,
text=t,
font=('Arial', 20),
fg=color,
bd=0.5,
command = lambda:press(t)
)
btn7.place(x=x, y=y, width=width, height=height)
btn('AC', 0, 230, color="#FF00FF")
btn('DEL', 70, 230, color="#FF00FF")
btn('/', 140, 230, color="#6A5ACD")
btn('*', 210, 230, color="#6A5ACD")
btn('7', 0, 285)
btn('8', 70, 285)
btn('9', 140, 285)
btn('-', 210, 285, color="#6A5ACD")
btn('4', 0, 340)
btn('5', 70, 340)
btn('6', 140, 340)
btn('+', 210, 340, color="#6A5ACD")
btn('1', 0, 395)
btn('2', 70, 395)
btn('3', 140, 395)
btn('=', 210, 395, color="#6A5ACD",height=110)
btn('%', 0, 450,width = 35)
btn('.', 35, 450,width = 35)
btn('0', 70, 450)
btn('(', 140, 450,width = 35)
btn(')', 175, 450,width = 35)
p.mainloop()
2.0
import tkinter as tk
def press(num):
global express
express = express + num
label2['text'] = express
def equal():
global express
result = eval(express)
label1['text'] = result
def AC():
global express
label1['text'] = ""
label2['text'] = '大爷你再输入一次'
express = ""
def DEL():
global express
express = express[0:len(express) - 1]
label1['text'] = ""
label2['text'] = express
def btn(t, x, y, color="#0000FF", width=70, height=55):
btn7 = tk.Button(
p,
text=t,
font=('Arial', 20),
fg=color,
bd=0.5,
command=lambda: press(t)
)
btn7.place(x=x, y=y, width=width, height=height)
p = tk.Tk()
p.geometry("280x500")
p.title("德钦计算器")
label1 = tk.Label(
p,
font=('Arial', 20),
bg="#8A2BE2",
fg="#000000",
bd=9,
anchor='se',
text="计算结果"
)
label1.place(x=0, y=0, width=280, height=170)
label2 = tk.Label(
p,
font=('Arial', 20),
bg="#8A2BF5",
fg="#000000",
bd=9,
anchor='se',
text="计算公式"
)
label2.place(x=0, y=170, width=280, height=60)
express = ""
btnA = tk.Button(
p,
text='AC',
font=('Arial', 20),
fg="#FF00FF",
bd=0.5,
command=AC
)
btnA.place(x=0, y=230, width=70, height=55)
btnD = tk.Button(
p,
text='DEL',
font=('Arial', 20),
fg="#FF00FF",
bd=0.5,
command=DEL
)
btnD.place(x=70, y=230, width=70, height=55)
btn('/', 140, 230, color="#6A5ACD")
btn('*', 210, 230, color="#6A5ACD")
btn('7', 0, 285)
btn('8', 70, 285)
btn('9', 140, 285)
btn('-', 210, 285, color="#6A5ACD")
btn('4', 0, 340)
btn('5', 70, 340)
btn('6', 140, 340)
btn('+', 210, 340, color="#6A5ACD")
btn('1', 0, 395)
btn('2', 70, 395)
btn('3', 140, 395)
btnE = tk.Button(
p,
text='=',
font=('Arial', 20),
fg="#6A5ACD",
bd=0.5,
command=equal
)
btnE.place(x=210, y=395, width=70, height=110)
btn('%', 0, 450, width=35)
btn('.', 35, 450, width=35)
btn('0', 70, 450)
btn('(', 140, 450, width=35)
btn(')', 175, 450, width=35)
p.mainloop()