Update 计算器.py

pull/1/head
plhj9vtan 6 months ago
parent a69f81aa82
commit 8cccef1f1e

@ -12,22 +12,23 @@ window.resizable(False, False)
# 设置窗口标题
window.title('计算器')
# 背景颜色
window.config(background="blue")
#window.config(background="BlueViolet")
window.config(background="Blue")
# 自动刷新字符串变量,可用 set 和 get 方法进行传值和取值
content_var = tk.StringVar(window, '')
# 创建单行文本框
content_entry = tk.Entry(window, textvariable=content_var)
content_entry = tk.Entry(window, textvariable=content_var, bd=10)
# 设置文本框为只读
content_entry['state'] = 'readonly'
# 设置文本框坐标及宽高
content_entry.place(x=20, y=10, width=300, height=30)
content_entry.place(x=20, y=10, width=300, height=38)
record_text = tk.Text(window, wrap=tk.WORD, height=10)
record_text.place(x=320, y=50, width=180, height=240)
# 按钮显示内容
value = ['e^x','', 'π', '%', '//','ln',
'**','', '(', ')', '/','lg',
value = ['eˣ','π', '**', '%', '//','ln',
'','', '(', ')', '/','lg',
'x!','7', '8', '9', '*','tan',
'','4', '5', '6', '-','cos',
'','1', '2', '3', '+','sin',]
@ -42,46 +43,60 @@ for row in range(5):
d = value[index]
index += 1
if (row==1 and (col==1 or col==2 or col==3)):
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="gray", fg="black")
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="gray", fg="black")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
elif (row==2 and (col==1 or col==2 or col==3)):
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
elif (row==3 and (col==1 or col==2 or col==3)):
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
elif (row==4 and (col==1 or col==2 or col==3)):
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="LightPink", fg="red")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
elif (col==5):
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="orange", fg="blue")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
else:
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="DeepSkyBlue", fg="purple")
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="DeepSkyBlue", fg="purple")
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
for l in range(5):
c = value0[index0]
index0 += 1
if (l == 2):
btn_digit = tk.Button(window, text=c, command=lambda x=c: onclick(x), bg="LightPink", fg="red")
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="LightPink", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40)
elif (l == 0):
btn_digit = tk.Button(window, text=c, command=lambda x=c: onclick(x), bg="SpringGreen", fg="red")
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="SpringGreen", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40)
elif (l == 1):
btn_digit = tk.Button(window, text=c, command=lambda x=c: onclick(x), bg="yellow", fg="red")
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="yellow", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40)
elif (l == 3):
btn_digit = tk.Button(window, text=c, command=lambda x=c: onclick(x), bg="SkyBlue", fg="red")
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="SkyBlue", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40)
else:
btn_digit = tk.Button(window, text=c, command=lambda x=c: onclick(x), bg="LightCoral", fg="black")
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="LightCoral", fg="black")
btn_digit.place(x=220, y=250, width=100, height=40)
lab_text_record = tk.Label(window, text='历史记录', font=("Helvetica", 14, "bold"), bg="white", fg="black")
lab_text_record.place(x=320, y=10, width=180, height=40)
# 添加清除历史记录按钮
btn_clear_record = tk.Button(window, text='清除记录', command=lambda: record_text.delete(1.0, tk.END), bg="white", fg="black")
btn_clear_record = tk.Button(window, text='清除记录', font=("Helvetica", 14, "bold"),
command=lambda: record_text.delete(1.0, tk.END), bg="white", fg="black")
btn_clear_record.place(x=320, y=250, width=180, height=40)
# 点击事件
@ -145,9 +160,10 @@ def onclick(key):
else:
tk.messagebox.showerror('错误', '在实数范围内,负数不能算数平方根!')
return
elif key == 'e^x':
elif key == 'eˣ':
# 添加常数e的x次方
content = exp(eval(content))
#content = exp(eval(content))
content += 'e**'
elif key == '':
# 删除最后一个字符
content = content[:-1]
@ -183,15 +199,20 @@ def onclick(key):
# 添加常数π
content += 'π'
elif key == 'sin':
content = sin(eval(content))
#content = sin(eval(content))
content += 'sin('
elif key == 'cos':
content = cos(eval(content))
#content = cos(eval(content))
content += 'cos('
elif key == 'tan':
content = tan(eval(content))
#content = tan(eval(content))
content += 'tan('
elif key == 'lg':
content = log10(eval(content))
#content = log10(eval(content))
content += 'log10('
elif key == 'ln':
content = log(eval(content))
#content = log(eval(content))
content += 'log('
elif key == 'rad':
content = str(eval(content)) + ' rad'
@ -201,5 +222,4 @@ def onclick(key):
# 运行主循环
window.mainloop()
# 2 + 9 * ((3*12) - 8) // 10 =
# 2 + 9 * ((3*12) - 8) // 10 = 27

Loading…
Cancel
Save