|
|
@ -1,13 +1,12 @@
|
|
|
|
import tkinter as tk
|
|
|
|
import tkinter as tk
|
|
|
|
from tkinter import messagebox
|
|
|
|
from tkinter import messagebox
|
|
|
|
import re
|
|
|
|
import re
|
|
|
|
#import math
|
|
|
|
|
|
|
|
from math import *
|
|
|
|
from math import *
|
|
|
|
|
|
|
|
|
|
|
|
# 创建主窗口
|
|
|
|
# 创建主窗口
|
|
|
|
window = tk.Tk()
|
|
|
|
window = tk.Tk()
|
|
|
|
# 设置窗口大小和位置
|
|
|
|
# 设置窗口大小和位置
|
|
|
|
window.geometry('340x240+600+300')
|
|
|
|
window.geometry('520x300+600+300')
|
|
|
|
# 不允许改变窗口大小
|
|
|
|
# 不允许改变窗口大小
|
|
|
|
window.resizable(False, False)
|
|
|
|
window.resizable(False, False)
|
|
|
|
# 设置窗口标题
|
|
|
|
# 设置窗口标题
|
|
|
@ -22,40 +21,68 @@ content_entry = tk.Entry(window, textvariable=content_var)
|
|
|
|
content_entry['state'] = 'readonly'
|
|
|
|
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=30)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
record_text = tk.Text(window, wrap=tk.WORD, height=10)
|
|
|
|
|
|
|
|
record_text.place(x=320, y=50, width=180, height=240)
|
|
|
|
|
|
|
|
|
|
|
|
# 按钮显示内容
|
|
|
|
# 按钮显示内容
|
|
|
|
value = ['e^x','√', 'π', '%', '//',
|
|
|
|
value = ['e^x','√', 'π', '%', '//','ln',
|
|
|
|
'**','⌫', '(', ')', '/',
|
|
|
|
'**','⌫', '(', ')', '/','lg',
|
|
|
|
'x!','7', '8', '9', '*',
|
|
|
|
'x!','7', '8', '9', '*','tan',
|
|
|
|
'x³','4', '5', '6', '-',
|
|
|
|
'x³','4', '5', '6', '-','cos',
|
|
|
|
'x²','1', '2', '3', '+',
|
|
|
|
'x²','1', '2', '3', '+','sin',]
|
|
|
|
'C','+/-', '0', '.', '=']
|
|
|
|
|
|
|
|
|
|
|
|
value0 = ['+/-','C', '0', '.', '=',]
|
|
|
|
|
|
|
|
|
|
|
|
index = 0
|
|
|
|
index = 0
|
|
|
|
# 将按钮进行 6x5 放置
|
|
|
|
index0 = 0
|
|
|
|
for row in range(6):
|
|
|
|
# 将按钮进行 5x6 放置
|
|
|
|
for col in range(5):
|
|
|
|
for row in range(5):
|
|
|
|
|
|
|
|
for col in range(6):
|
|
|
|
d = value[index]
|
|
|
|
d = value[index]
|
|
|
|
index += 1
|
|
|
|
index += 1
|
|
|
|
if (row==1 and (col==1 or col==2 or col==3)):
|
|
|
|
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, command=lambda x=d: onclick(x), bg="gray", fg="black")
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
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)):
|
|
|
|
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, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
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)):
|
|
|
|
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, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
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)):
|
|
|
|
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, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
btn_digit.place(x=20 + col * 50, y=50 + row * 40, width=50, height=40)
|
|
|
|
elif (row==5 and col==2):
|
|
|
|
|
|
|
|
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="LightPink", fg="red")
|
|
|
|
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
|
|
|
|
elif (row==5 and col!=2):
|
|
|
|
|
|
|
|
btn_digit = tk.Button(window, text=d, command=lambda x=d: onclick(x), bg="SpringGreen", fg="red")
|
|
|
|
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
|
|
|
|
else:
|
|
|
|
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, command=lambda x=d: onclick(x), bg="DeepSkyBlue", fg="purple")
|
|
|
|
btn_digit.place(x=20 + col * 62, y=50 + row * 30, width=50, height=20)
|
|
|
|
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.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.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.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.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.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.place(x=320, y=250, width=180, height=40)
|
|
|
|
|
|
|
|
|
|
|
|
# 点击事件
|
|
|
|
# 点击事件
|
|
|
|
def onclick(key):
|
|
|
|
def onclick(key):
|
|
|
@ -75,7 +102,7 @@ def onclick(key):
|
|
|
|
last_part = re.split(r'[+\-*/]', content)[-1]
|
|
|
|
last_part = re.split(r'[+\-*/]', content)[-1]
|
|
|
|
if '.' in last_part:
|
|
|
|
if '.' in last_part:
|
|
|
|
# 信息提示对话框
|
|
|
|
# 信息提示对话框
|
|
|
|
tk.messagebox.showerror('Error', 'Repeated decimal point')
|
|
|
|
tk.messagebox.showerror('错误', '数字只有一个小数点,所以不能再次点击!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
content += key
|
|
|
|
content += key
|
|
|
@ -85,17 +112,18 @@ def onclick(key):
|
|
|
|
elif key == '=':
|
|
|
|
elif key == '=':
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
# 对输入的表达式求值
|
|
|
|
# 对输入的表达式求值
|
|
|
|
content = str(eval(content.replace("π", str(pi))))
|
|
|
|
result = str(eval(content.replace("π", str(pi)).replace("rad", "radians")))
|
|
|
|
content = str(eval(content))
|
|
|
|
record_text.insert(tk.END, f"{content} = {result}\n")
|
|
|
|
|
|
|
|
content = result
|
|
|
|
except ZeroDivisionError:
|
|
|
|
except ZeroDivisionError:
|
|
|
|
tk.messagebox.showerror('Error', 'Division by zero is not allowed')
|
|
|
|
tk.messagebox.showerror('错误', '这个值太大了,以至于无法算出其值,0不能作为除数!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
tk.messagebox.showerror('Error', f'Invalid expression: {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('Error', 'Consecutive operators not allowed')
|
|
|
|
tk.messagebox.showerror('错误', '运算符号之间不能直接连接,需要加入数值!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
content += key
|
|
|
|
content += key
|
|
|
|
|
|
|
|
|
|
|
@ -115,7 +143,7 @@ def onclick(key):
|
|
|
|
if all(map(lambda x: x.isdigit(), n)):
|
|
|
|
if all(map(lambda x: x.isdigit(), n)):
|
|
|
|
content = sqrt(eval(content))
|
|
|
|
content = sqrt(eval(content))
|
|
|
|
else:
|
|
|
|
else:
|
|
|
|
tk.messagebox.showerror('Error', 'Invalid expression')
|
|
|
|
tk.messagebox.showerror('错误', '在实数范围内,负数不能算数平方根!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
elif key == 'e^x':
|
|
|
|
elif key == 'e^x':
|
|
|
|
# 添加常数e的x次方
|
|
|
|
# 添加常数e的x次方
|
|
|
@ -128,32 +156,44 @@ def onclick(key):
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
content = str(float(content) ** 2)
|
|
|
|
content = str(float(content) ** 2)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
tk.messagebox.showerror('Error', f'Invalid expression: {e}')
|
|
|
|
tk.messagebox.showerror('错误', f'无效表达式: {e}')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
elif key == 'x³':
|
|
|
|
elif key == 'x³':
|
|
|
|
# 计算立方
|
|
|
|
# 计算立方
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
content = str(float(content) ** 3)
|
|
|
|
content = str(float(content) ** 3)
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
tk.messagebox.showerror('Error', f'Invalid expression: {e}')
|
|
|
|
tk.messagebox.showerror('错误', f'无效表达式: {e}')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
elif key == 'x!':
|
|
|
|
elif key == 'x!':
|
|
|
|
# 计算阶乘
|
|
|
|
# 计算阶乘
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
num = int(content)
|
|
|
|
num = int(content)
|
|
|
|
if num < 0:
|
|
|
|
if num < 0:
|
|
|
|
tk.messagebox.showerror('Error', 'Please enter a non-negative integer')
|
|
|
|
tk.messagebox.showerror('错误', '请输入一个非负整数进行阶乘运算!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
content = str(factorial(num))
|
|
|
|
content = str(factorial(num))
|
|
|
|
except ValueError:
|
|
|
|
except ValueError:
|
|
|
|
tk.messagebox.showerror('Error', 'Please enter a non-negative integer')
|
|
|
|
tk.messagebox.showerror('错误', '请输入一个非负整数进行阶乘运算!')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
except Exception as e:
|
|
|
|
except Exception as e:
|
|
|
|
tk.messagebox.showerror('Error', f'Invalid expression: {e}')
|
|
|
|
tk.messagebox.showerror('错误', f'无效的表达式: {e}')
|
|
|
|
return
|
|
|
|
return
|
|
|
|
elif key == 'π':
|
|
|
|
elif key == 'π':
|
|
|
|
# 添加常数π
|
|
|
|
# 添加常数π
|
|
|
|
content += 'π'
|
|
|
|
content += 'π'
|
|
|
|
|
|
|
|
elif key == 'sin':
|
|
|
|
|
|
|
|
content = sin(eval(content))
|
|
|
|
|
|
|
|
elif key == 'cos':
|
|
|
|
|
|
|
|
content = cos(eval(content))
|
|
|
|
|
|
|
|
elif key == 'tan':
|
|
|
|
|
|
|
|
content = tan(eval(content))
|
|
|
|
|
|
|
|
elif key == 'lg':
|
|
|
|
|
|
|
|
content = log10(eval(content))
|
|
|
|
|
|
|
|
elif key == 'ln':
|
|
|
|
|
|
|
|
content = log(eval(content))
|
|
|
|
|
|
|
|
elif key == 'rad':
|
|
|
|
|
|
|
|
content = str(eval(content)) + ' rad'
|
|
|
|
|
|
|
|
|
|
|
|
# 将结果显示到文本框中
|
|
|
|
# 将结果显示到文本框中
|
|
|
|
content_var.set(content)
|
|
|
|
content_var.set(content)
|
|
|
@ -161,5 +201,5 @@ def onclick(key):
|
|
|
|
# 运行主循环
|
|
|
|
# 运行主循环
|
|
|
|
window.mainloop()
|
|
|
|
window.mainloop()
|
|
|
|
|
|
|
|
|
|
|
|
#x = 2 + 9 * ((3*12) - 8) // 10
|
|
|
|
# 2 + 9 * ((3*12) - 8) // 10 =
|
|
|
|
#print(x)
|
|
|
|
|
|
|
|