import tkinter as tk
window = tk.Tk()
window.geometry("280x500")
window.title("计算器")
# 显示结果
la1 = tk.Label(
window,
font=(None, 30),
bg="#0000CD",
fg="white",
bd=1,
anchor="se", # 锚 north北 east 东 west西 south 南
text="显示结果"
)
la1.place(x=0, y=0, width=280, height=170)
# 显示公式
la2 = tk.Label(
window,
font=(None, 30),
bg="#0000FF",
fg="#7B68EE",
bd=1,
anchor="se",
text="显示公式"
)
la2.place(x=0, y=170, width=280, height=60)
suan_shi = ""
def gong_shi(t):
global suan_shi
suan_shi = suan_shi + t
la2["text"] = suan_shi
def deng_yu():
la1["text"] = eval(suan_shi)
def qing_chu():
global suan_shi
suan_shi = ""
la2["text"] = suan_shi
la1["text"] = "一给我里giao"
def shan_chu():
global suan_shi
# 查找算式的第一个字符到倒数第二个字符
suan_shi = suan_shi[0:-1]
# 将新的算式放到计算器窗口上
la2["text"] = suan_shi
# 写一个按钮
def btn(t, x, y, w=70, h=55):
gong_neng = lambda: gong_shi(t)
if t == "=":
gong_neng = lambda: deng_yu()
elif t == "AC":
gong_neng = lambda: qing_chu()
elif t == "←":
gong_neng = lambda: shan_chu()
btnAC = tk.Button(
window,
text=t,
font=(None, 20),
fg="#00BFFF",
command=gong_neng
)
btnAC.place(x=x, y=y, width=w, height=h)
# 第一行按钮
btn("AC", 0, 230)
btn("←", 70, 230)
btn("/", 140, 230)
btn("*", 210, 230)
# 第二行按钮
btn("7", 0, 285)
btn("8", 70, 285)
btn("9", 140, 285)
btn("-", 210, 285)
# 第三行按钮
btn("4", 0, 340)
btn("5", 70, 340)
btn("6", 140, 340)
btn("+", 210, 340)
# 第四行按钮
btn("1", 0, 395)
btn("2", 70, 395)
btn("3", 140, 395)
btn("=", 210, 395, h=110)
# 第五行按钮
btn("%", 0, 450, w=35)
btn(".", 35, 450, w=35)
btn("0", 70, 450)
btn("(", 140, 450, w=35)
btn(")", 175, 450, w=35)
window.mainloop()
ziheng -计算器2.0
最新推荐文章于 2021-04-09 23:15:29 发布