Update 计算器.py

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

@ -11,8 +11,7 @@ window.geometry('520x300+600+300')
window.resizable(False, False) window.resizable(False, False)
# 设置窗口标题 # 设置窗口标题
window.title('计算器') window.title('计算器')
# 背景颜色 # 背景颜色,例window.config(background="BlueViolet")
#window.config(background="BlueViolet")
window.config(background="Blue") window.config(background="Blue")
# 自动刷新字符串变量,可用 set 和 get 方法进行传值和取值 # 自动刷新字符串变量,可用 set 和 get 方法进行传值和取值
content_var = tk.StringVar(window, '') content_var = tk.StringVar(window, '')
@ -22,70 +21,88 @@ content_entry = tk.Entry(window, textvariable=content_var, bd=10)
content_entry['state'] = 'readonly' content_entry['state'] = 'readonly'
# 设置文本框坐标及宽高 # 设置文本框坐标及宽高
content_entry.place(x=20, y=10, width=300, height=38) content_entry.place(x=20, y=10, width=300, height=38)
# 记录计算过程
record_text = tk.Text(window, wrap=tk.WORD, height=10) record_text = tk.Text(window, wrap=tk.WORD, height=10)
record_text.place(x=320, y=50, width=180, height=240) record_text.place(x=320, y=50, width=180, height=240)
# 按钮显示内容 # 按钮显示内容
value = ['','π', '**', '%', '//','ln', values = ['', 'π', '**', '%', '//', 'ln',
'','', '(', ')', '/','lg', '', '', '(', ')', '/', 'lg',
'x!','7', '8', '9', '*','tan', 'x!', '7', '8', '9', '*', 'tan',
'','4', '5', '6', '-','cos', '', '4', '5', '6', '-', 'cos',
'','1', '2', '3', '+','sin',] '', '1', '2', '3', '+', 'sin']
value0 = ['+/-','C', '0', '.', '=',] # 其余特殊按钮
value = ['+/-', 'C', '0', '.', '=']
index_s = 0
index = 0 index = 0
index0 = 0
# 将按钮进行 5x6 放置 # 将按钮进行 5x6 放置
for row in range(5): for row in range(5):
for col in range(6): for col in range(6):
d = value[index] d = values[index_s]
index += 1 index_s += 1
if (row==1 and (col==1 or col==2 or col==3)): if row == 1:
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"), if col == 1 or col == 2 or col == 3:
command=lambda x=d: onclick(x), bg="gray", fg="black") btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40) command=lambda x=d: onclick(x), bg="gray", fg="black")
elif (row==2 and (col==1 or col==2 or col==3)): btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"), elif col == 0 or col == 4:
command=lambda x=d: onclick(x), bg="LightPink", fg="red") btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40) command=lambda x=d: onclick(x), bg="DeepSkyBlue", fg="purple")
elif (row==3 and (col==1 or col==2 or col==3)): btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"), else:
command=lambda x=d: onclick(x), bg="LightPink", fg="red") btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40) command=lambda x=d: onclick(x), bg="orange", fg="blue")
elif (row==4 and (col==1 or col==2 or col==3)): btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="LightPink", fg="red") elif row == 2 or row == 3 or row == 4:
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40) if col == 1 or col == 2 or col == 3:
elif (col==5): 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 == 0 or col == 4:
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)
else:
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)
elif col == 5:
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="orange", fg="blue") 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) btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
else: else:
btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=d, font=("Helvetica", 14, "bold"),
command=lambda x=d: onclick(x), bg="DeepSkyBlue", fg="purple") 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) btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
for l in range(5): for leo in range(5):
c = value0[index0] c = value[index]
index0 += 1 index += 1
if (l == 2): if leo == 2:
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="LightPink", fg="red") command=lambda x=c: onclick(x), bg="LightPink", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40) btn_digit.place(x=20 + leo * 50, y=250, width=50, height=40)
elif (l == 0):
elif leo == 0:
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="SpringGreen", fg="red") command=lambda x=c: onclick(x), bg="SpringGreen", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40) btn_digit.place(x=20 + leo * 50, y=250, width=50, height=40)
elif (l == 1):
elif leo == 1:
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="yellow", fg="red") command=lambda x=c: onclick(x), bg="yellow", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40) btn_digit.place(x=20 + leo * 50, y=250, width=50, height=40)
elif (l == 3):
elif leo == 3:
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="SkyBlue", fg="red") command=lambda x=c: onclick(x), bg="SkyBlue", fg="red")
btn_digit.place(x=20 + l * 50, y=250, width=50, height=40) btn_digit.place(x=20 + leo * 50, y=250, width=50, height=40)
else: else:
btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"), btn_digit = tk.Button(window, text=c, font=("Helvetica", 14, "bold"),
command=lambda x=c: onclick(x), bg="LightCoral", fg="black") command=lambda x=c: onclick(x), bg="LightCoral", fg="black")
@ -99,6 +116,7 @@ btn_clear_record = tk.Button(window, text='清除记录', font=("Helvetica", 14,
command=lambda: record_text.delete(1.0, tk.END), bg="white", fg="black") 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) btn_clear_record.place(x=320, y=250, width=180, height=40)
# 点击事件 # 点击事件
def onclick(key): def onclick(key):
# 运算符 # 运算符
@ -112,6 +130,7 @@ def onclick(key):
if key in '0123456789': if key in '0123456789':
# 按下 0-9 在 content 中追加 # 按下 0-9 在 content 中追加
content += key content += key
elif key == '.': elif key == '.':
# 将 content 从 +-*/ 这些字符的地方分割开来 # 将 content 从 +-*/ 这些字符的地方分割开来
last_part = re.split(r'[+\-*/]', content)[-1] last_part = re.split(r'[+\-*/]', content)[-1]
@ -121,9 +140,11 @@ def onclick(key):
return return
else: else:
content += key content += key
elif key == 'C': elif key == 'C':
# 清除文本框 # 清除文本框
content = '' content = ''
elif key == '=': elif key == '=':
try: try:
# 对输入的表达式求值 # 对输入的表达式求值
@ -136,6 +157,7 @@ def onclick(key):
except Exception as e: except Exception as e:
tk.messagebox.showerror('错误', f'无效表达式: {e}') tk.messagebox.showerror('错误', f'无效表达式: {e}')
return return
elif key in operation: elif key in operation:
if content.endswith(operation): if content.endswith(operation):
tk.messagebox.showerror('错误', '运算符号之间不能直接连接,需要加入数值!') tk.messagebox.showerror('错误', '运算符号之间不能直接连接,需要加入数值!')
@ -148,9 +170,11 @@ def onclick(key):
content = content[1:] content = content[1:]
else: else:
content = '-' + content content = '-' + content
elif key in '()': elif key in '()':
# 直接在内容后面追加括号 # 直接在内容后面追加括号
content += key content += key
elif key == '': elif key == '':
# 从 . 处分割存入 nn 是一个列表 # 从 . 处分割存入 nn 是一个列表
n = content.split('.') n = content.split('.')
@ -160,13 +184,15 @@ def onclick(key):
else: else:
tk.messagebox.showerror('错误', '在实数范围内,负数不能算数平方根!') tk.messagebox.showerror('错误', '在实数范围内,负数不能算数平方根!')
return return
elif key == '': elif key == '':
# 添加常数e的x次方 # 添加常数e的x次方,content = exp(eval(content))
#content = exp(eval(content))
content += 'e**' content += 'e**'
elif key == '': elif key == '':
# 删除最后一个字符 # 删除最后一个字符
content = content[:-1] content = content[:-1]
elif key == '': elif key == '':
# 计算平方 # 计算平方
try: try:
@ -174,6 +200,7 @@ def onclick(key):
except Exception as e: except Exception as e:
tk.messagebox.showerror('错误', f'无效表达式: {e}') tk.messagebox.showerror('错误', f'无效表达式: {e}')
return return
elif key == '': elif key == '':
# 计算立方 # 计算立方
try: try:
@ -181,6 +208,7 @@ def onclick(key):
except Exception as e: except Exception as e:
tk.messagebox.showerror('错误', f'无效表达式: {e}') tk.messagebox.showerror('错误', f'无效表达式: {e}')
return return
elif key == 'x!': elif key == 'x!':
# 计算阶乘 # 计算阶乘
try: try:
@ -195,31 +223,43 @@ def onclick(key):
except Exception as e: except Exception as e:
tk.messagebox.showerror('错误', f'无效的表达式: {e}') tk.messagebox.showerror('错误', f'无效的表达式: {e}')
return return
elif key == 'π': elif key == 'π':
# 添加常数π # 添加常数π
content += 'π' content += 'π'
elif key == 'sin': elif key == 'sin':
#content = sin(eval(content)) # 计算sin值,content = sin(eval(content))
content += 'sin(' content += 'sin('
elif key == 'cos': elif key == 'cos':
#content = cos(eval(content)) # 计算cos值,content = cos(eval(content))
content += 'cos(' content += 'cos('
elif key == 'tan': elif key == 'tan':
#content = tan(eval(content)) # 计算tan值,content = tan(eval(content))
content += 'tan(' content += 'tan('
elif key == 'lg': elif key == 'lg':
#content = log10(eval(content)) # 计算常数对数lg的值,content = log10(eval(content))
content += 'log10(' content += 'log10('
elif key == 'ln': elif key == 'ln':
#content = log(eval(content)) # 计算自然对数ln的值,content = log(eval(content))
content += 'log(' content += 'log('
elif key == 'rad': elif key == 'rad':
# 当输入数字到三角函数运算时,默认为弧度制。
content = str(eval(content)) + ' rad' content = str(eval(content)) + ' rad'
# 将结果显示到文本框中 # 将结果显示到文本框中
content_var.set(content) content_var.set(content)
# 运行主循环 # 运行主循环
window.mainloop() window.mainloop()
# 2 + 9 * ((3*12) - 8) // 10 = 27 # 2 + 9 * ((3*12) - 8) // 10 = 27
# 3 * 4 ** 2 // 8 % 7 = 6
# 3 + 5 % 6 * 2 // 8 = 4

Loading…
Cancel
Save