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