from tkinter import messagebox
import math
class Calculator:
def __init__(self, root):
self.root = root
self.root.title("Full-Featured Calculator")
self.root.geometry("400x600")
self.root.resizable(False, False)
# Mode variable (degrees or radians)
self.mode = tk.StringVar(value="RAD")
# Create display
self.display = tk.Entry(self.root, width=30, font=("Arial", 14), borderwidth=5, justify="right")
self.display.grid(row=0, column=0, columnspan=5, padx=10, pady=10)
# Create mode selection
mode_frame = tk.Frame(self.root)
mode_frame.grid(row=1, column=0, columnspan=5, pady=5)
tk.Radiobutton(mode_frame, text="DEG", variable=self.mode, value="DEG").pack(side="left", padx=5)
tk.Radiobutton(mode_frame, text="RAD", variable=self.mode, value="RAD").pack(side="left", padx=5)
# Define buttons
buttons = [
('C', 'C', 0), ('←', 'BACK', 1), ('(', '(', 2), (')', ')', 3), ('÷', '/', 4),
('7', '7', 0), ('8', '8', 1), ('9', '9', 2), ('×', '*', 3), ('√', 'math.sqrt(', 4),
('4', '4', 0), ('5', '5', 1), ('6', '6', 2), ('−', '-', 3), ('sin', 'sin', 4),
('1', '1', 0), ('2', '2', 1), ('3', '3', 2), ('+', '+', 3), ('cos', 'cos', 4),
('0', '0', 0), ('.', '.', 1), ('=', '=', 2), ('^', '**', 3), ('tan', 'tan', 4),
('π', 'math.pi', 0), ('e', 'math.e', 1), ('ln', 'math.log(', 2), ('log', 'math.log10(', 3), ('asin', 'asin', 4)
]
# Create buttons
for label, value, col in buttons:
tk.Button(self.root, text=label, width=5, height=2, font=("Arial", 12),
command=lambda v=value: self.button_click(v)).grid(row=2 + (buttons.index((label, value, col)) // 5), column=col, padx=5, pady=5)
def button_click(self, value):
"""Handle button clicks"""
current = self.display.get()
if value == 'C':
self.display.delete(0, tk.END)
elif value == 'BACK':
self.display.delete(len(current) - 1, tk.END)
elif value == '=':
try:
# Prepare safe evaluation environment
result = eval(current, {"__builtins__": None}, {"math": math, "sin": self.sin, "cos": self.cos, "tan": self.tan,
"asin": self.asin, "acos": self.acos, "atan": self.atan})
self.display.delete(0, tk.END)
self.display.insert(0, str(round(result, 10)))
except Exception:
messagebox.showerror("错误", "无效的表达式")
self.display.delete(0, tk.END)
self.display.insert(0, "Error")
else:
self.display.insert(tk.END, value)
def sin(self, x):
"""Sine function with mode handling"""
return math.sin(math.radians(x) if self.mode.get() == "DEG" else x)
def cos(self, x):
"""Cosine function with mode handling"""
return math.cos(math.radians(x) if self.mode.get() == "DEG" else x)
def tan(self, x):
"""Tangent function with mode handling"""
return math.tan(math.radians(x) if self.mode.get() == "DEG" else x)
def asin(self, x):
"""Arcsine function with mode handling"""
result = math.asin(x)
return math.degrees(result) if self.mode.get() == "DEG" else result
def acos(self, x):
"""Arccosine function with mode handling"""
result = math.acos(x)
return math.degrees(result) if self.mode.get() == "DEG" else result
def atan(self, x):
"""Arctangent function with mode handling"""
result = math.atan(x)
return math.degrees(result) if self.mode.get() == "DEG" else result
if __name__ == "__main__":
root = tk.Tk()
app = Calculator(root)
root.mainloop()

2025-6-23 22-4-56.png (15.92 KB, 下载次数: 0)
下载附件
2025-6-23 22:05 上传
exe文件下载: https://wwtg.lanzouo.com/iUXuw2zfbdcj