让AI写了一个全功能计算器的代码

查看 75|回复 8
作者:blackedyou   
[Python] 纯文本查看 复制代码import tkinter as tk
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

写了, 代码

Motivar   

感谢分享
hicubee   

现在的AI确实能力强过很多人了, 但会用AI的前提也得会输入正确的提示词, 很赞!
m_h   

我个人觉得 这个好用  ,Qalculate!
blackedyou
OP
  


m_h 发表于 2025-6-23 22:38
我个人觉得 这个好用  ,Qalculate!

这个更专业,AI可能偷懒了
sjx85673030   

还不错,现在AI太牛了
jtjt68   

谢谢分享原创作品
123tiankongli   

学习一下,AI挺有意思的
clickcheck   

能用AI写出一个可用的程序,支持一下
您需要登录后才可以回帖 登录 | 立即注册

返回顶部