AIGC(生成式AI)试用 7 — 桌面小程序

    生成式AI,别人用来写作,我先用来写个桌面小程序。

    桌面小程序:计算器
    需求

  1. Python开发
  2. 图形界面,标题:计算器
  3. + – * / 基本运算
  4. 计算范围:-999999999 ~ 999999999
  5. ** 乘方计算(例,2**3=2*2*2=8)
  6. 记录并显示输入及计算结果日志

– 文心一言的上下文继承,可以对上一问题结合当前提问给出自己的理解

  • 【计算器小程序】:CSDN创作助手 文心一言都给出了python命令行下的基本计算器过程,没有图形界面
  • 【Python开发】:给出的均是python代码,无需多言,也许python是现在大家的忠爱,其他语言呢?
  • 【图形界面,标题:计算器】:CSDN创作助手无法给出明确答案,“对不起”;文心一言直接给出了python + tk的基础代码
  • 【计算器小程序,图形界面,标题:计算器】:CSDN创作助手不理解,再给的细致些,还好理解了,给出了基本的设计原则
  • 【+ – * / 基本运算】:不做过多描述,CSDB创作助手 和 文心一言从提出计算器小程序的问题开始就实现了这些基本的操作
  • 【计算范围:-999999999 ~ 999999999】:CSDN创作助手仍是抱歉;文心一言继承了上下文的内容给出了操作,但更多的条件并没有带来更多的内容,界面简洁了不少,但9个9的限制并没有起到作用
  • 【计算器小程序,图形界面,标题:计算器,** 乘方计算(例,2**3=2*2*2=8)】:CSDN创作助手给出了任意输入的界面,通过输入直接生成计算结果,显的有些简陋,但实现了提问的**乘方的计算生成;文心一言给出了简洁的界面,并实现了**乘方的算法;– 利用了python的乘方计算 ** ?试试别的看
  • 【计算器小程序,图形界面,标题:计算器,*** 乘方计算(例,2***3=2*2*2=8)】:CSDN创作助手未能给出具体的代码,也未能处理乘方由**到***的转换;文心一言给出的代码并未有变化,乘方由**到***的并未能给出正确的处理,ERROR所给出的是更强的易用性
    说明:**是python自代运算,换个方式提问看生成式AI的反应
  • 【计算器小程序,图形界面,标题:计算器,** 乘方计算,记录并显示输入及计算结果日志】:CSDN创作助手并没有按提问给出图形界面,但对日志给出了相应的记录和显示方法;文心一言,没了主程序调用的代码,界面由于log的影响显得不太协调,需要人工二次处理
提问主题CSDN创作助手文心一言
计算器小程序
很好的主意!以下为你提供一个简单的计算器小程序,使用Python语言编写。

插入
def calculator():
    print(‘欢迎使用计算器’)
    num1 = float(input(‘请输入第一个数字:’))
    num2 = float(input(‘请输入第二个数字:’))
   
    print(‘\n1. 加法\n2. 减法\n3. 乘法\n4. 除法’)
    operation = int(input(‘\n请选择要进行的操作:’))
   
    if operation == 1:
        result = num1 + num2
        print(f’\n{num1} + {num2} = {result}’)
    elif operation == 2:
        result = num1 – num2
        print(f’\n{num1} – {num2} = {result}’)
    elif operation == 3:
        result = num1 * num2
        print(f’\n{num1} * {num2} = {result}’)
    elif operation == 4:
        if num2 == 0:
            print(‘错误:除数不能为0’)
        else:
            result = num1 / num2
            print(f’\n{num1} / {num2} = {result}’)
    else:
        print(‘错误:无效的操作符’)
        calculator()
   
    repeat = input(‘\n是否继续使用计算器?(y/n)’)
   
    if repeat.lower() == ‘y’:
        calculator()
    else:
        print(‘感谢使用计算器’)

calculator()

运行该程序后,用户将首先被要求输入两个数字。接下来,用户将被提示从四个选项中选择一个操作。程序将执行选定的操作并输出结果。然后用户将被询问是否要继续使用计算器,如果选择“y”,程序将在最开始的步骤重复。否则,程序将退出。

请注意,此程序为简单示例,可能需要进行更多测试和改进才能用于真实场景。

当然,我可以为你提供一个简单的计算器小程序。这里是一个基本的Python代码示例,你可以使用它来进行基本的数学运算。请注意,这只是一个简单的示例,你可以根据自己的需求进行修改和扩展。


python复制代码
import tkinter as tk

def evaluate(event):
    try:
        result = eval(entry.get())
        label.config(text = “结果:” + str(result))
    except:
        label.config(text = “无效输入”)

window = tk.Tk()
window.title(“计算器”)

entry = tk.Entry(window, width=30)
entry.bind(“<Return>”, evaluate)
entry.grid(row=0, column=0, columnspan=4)

button0 = tk.Button(window, text=”0″, width=5, command=lambda: entry.insert(tk.END, ‘0’))
button0.grid(row=4, column=1)
button1 = tk.Button(window, text=”1″, width=5, command=lambda: entry.insert(tk.END, ‘1’))
button1.grid(row=3, column=0)
button2 = tk.Button(window, text=”2″, width=5, command=lambda: entry.insert(tk.END, ‘2’))
button2.grid(row=3, column=1)
button3 = tk.Button(window, text=”3″, width=5, command=lambda: entry.insert(tk.END, ‘3’))
button3.grid(row=3, column=2)
button4 = tk.Button(window, text=”4″, width=5, command=lambda: entry.insert(tk.END, ‘4’))
button4.grid(row=2, column=0)
button5 = tk.Button(window, text=”5″, width=5, command=lambda: entry.insert(tk.END, ‘5’))
button5.grid(row=2, column=1)
button6 = tk.Button(window, text=”6″, width=5, command=lambda: entry.insert(tk.END, ‘6’))
button6.grid(row=2, column=2)
button7 = tk.Button(window, text=”7″, width=5, command=lambda: entry.insert(tk.END, ‘7’))
button7.grid(row=1, column=0)
button8 = tk.Button(window, text=”8″, width=5, command=lambda: entry.insert(tk.END, ‘8’))
button8.grid(row=1, column=1)
button9 = tk.Button(window, text=”9″, width=5, command=lambda: entry.insert(tk.END, ‘9’))
button9.grid(row=1, column=2)

plus = tk.Button(window, text=”+”, width=5, command=lambda: entry.insert(tk.END, ‘+’))
plus.grid(row=1, column=3)
minus = tk.Button(window, text=”-“, width=5, command=lambda: entry.insert(tk.END, ‘-‘))
minus.grid(row=2, column=3)
multiply = tk.Button(window, text=”*”, width=5, command=lambda: entry.insert(tk.END, ‘*’))
multiply.grid(row=3, column=3)
divide = tk.Button(window, text=”/”, width=5, command=lambda: entry.insert(tk.END, ‘/’))
divide.grid(row=4, column=3)
clear = tk.Button(window, text=”Clear”, width=5, command=lambda: entry.delete(0, tk.END))
clear.grid(row=4, column=0)
equal = tk.Button(window, text=”=”, width=5, command=lambda: evaluate(None))
equal.grid(row=4, column=2)

label = tk.Label(window, text=”输入表达式:”)
label.grid(row=5, column=0, columnspan=4)

window.mainloop()
这个小程序会提示用户选择操作,然后输入两个数字进行计算。注意,这个程序只处理两个数字的运算,如果你想要处理更复杂的表达式,你可能需要使用更复杂的解析和计算技术。

Python开发– 
图形界面,标题:计算器对不起,我是语言模型AI,我无法提供普通计算器的图形界面。

很高兴为您提供一个简单的Python图形界面计算器程序!请参考以下代码:


python复制代码
 import tkinter as tk
 
 def evaluate(event):
 try:
 result = eval(entry.get())
 label.config(text = “结果:” + str(result))
 except:
 label.config(text = “无效输入”)
 
 window = tk.Tk()
 window.title(“计算器”)
 
 entry = tk.Entry(window, width=30)
 entry.bind(“<Return>”, evaluate)
 entry.grid(row=0, column=0, columnspan=4)
 
 button0 = tk.Button(window, text=”0″, width=5, command=lambda: entry.insert(tk.END, ‘0’))
 button0.grid(row=4, column=1)
 button1 = tk.Button(window, text=”1″, width=5, command=lambda: entry.insert(tk.END, ‘1’))
 button1.grid(row=3, column=0)
 button2 = tk.Button(window, text=”2″, width=5, command=lambda: entry.insert(tk.END, ‘2’))
 button2.grid(row=3, column=1)
 button3 = tk.Button(window, text=”3″, width=5, command=lambda: entry.insert(tk.END, ‘3’))
 button3.grid(row=3, column=2)
 button4 = tk.Button(window, text=”4″, width=5, command=lambda: entry.insert(tk.END, ‘4’))
 button4.grid(row=2, column=0)
 button5 = tk.Button(window, text=”5″, width=5, command=lambda: entry.insert(tk.END, ‘5’))
 button5.grid(row=2, column=1)
 button6 = tk.Button(window, text=”6″, width=5, command=lambda: entry.insert(tk.END, ‘6’))
 button6.grid(row=2, column=2)
 button7 = tk.Button(window, text=”7″, width=5, command=lambda: entry.insert(tk.END, ‘7’))
 button7.grid(row=1, column=0)
 button8 = tk.Button(window, text=”8″, width=5, command=lambda: entry.insert(tk.END, ‘8’))
 button8.grid(row=1, column=1)
 button9 = tk.Button(window, text=”9″, width=5, command=lambda: entry.insert(tk.END, ‘9’))
 button9.grid(row=1, column=2)
 
 plus = tk.Button(window, text=”+”, width=5, command=lambda: entry.insert(tk.END, ‘+’))
 plus.grid(row=1, column=3)
 minus = tk.Button(window, text=”-“, width=5, command=lambda: entry.insert(tk.END, ‘-‘))
 minus.grid(row=2, column=3)
 multiply = tk.Button(window, text=”*”, width=5, command=lambda: entry.insert(tk.END, ‘*’))
 multiply.grid(row=3, column=3)
 divide = tk.Button(window, text=”/”, width=5, command=lambda: entry.insert(tk.END, ‘/’))
 divide.grid(row=4, column=3)
 clear = tk.Button(window, text=”Clear”, width=5, command=lambda: entry.delete(0, tk.END))
 clear.grid(row=4, column=0)
 equal = tk.Button(window, text=”=”, width=5, command=lambda: evaluate(None))
 equal.grid(row=4, column=2)
 
 label = tk.Label(window, text=”输入表达式:”)
 label.grid(row=5, column=0, columnspan=4)
 
 window.mainloop()
这个程序使用了Python的tkinter库来创建图形界面。它包含一个文本输入框和一个数字键盘,以及加、减、乘、除等运算按钮。您可以在输入框中输入一个算术表达式,然后点击等号按钮进行求值。结果将显示在标签中。

计算器小程序,图形界面,标题:计算器抱歉,我是一名语言模型人工智能,无法提供真实的图形界面或编程实现。但是,下面是计算器的基本操作和逻辑:

基本操作:

加法:输入两个数,点击“+”按钮,显示结果。
减法:输入两个数,点击“-”按钮,显示结果。
乘法:输入两个数,点击“×”按钮,显示结果。
除法:输入两个数,点击“÷”按钮,显示结果。
取余:输入两个数,点击“%”按钮,显示结果。
求平方:输入一个数,点击“x²”按钮,显示结果。
求平方根:输入一个数,点击“√”按钮,显示结果。
倒数:输入一个数,点击“1/x”按钮,显示结果。
逻辑:

获取用户输入的数字。
获取用户输入的运算符。
判断运算符,根据不同的运算符进行不同的计算。
显示计算结果。
以上逻辑可以通过编程实现,并通过图形界面呈现。
+ – * / 基本运算不用再提问,基本操作乍动实现不用再提问,基本操作乍动实现
计算范围:-999999999 ~ 999999999
抱歉,由于我是一个语言模型,无法为您提供图形界面以及完整的小程序。但是,我可以为您提供计算器的基本功能和计算范围:

计算器基本功能包括加、减、乘、除、取反、取模、开方、乘方、sin、cos、tan 等,您可以根据需求自行设计图形界面,用户可以输入数字和操作符,同时程序会输出计算结果,还可以处理错误输入。

计算范围为 -999999999 ~ 999999999,也就是所有运算符在此数字范围内的计算都是合法的,超出此范围则需要特殊处理。需要注意的是,浮点数计算需要额外注意精度问题。
好的,以下是一个简单的计算器小程序,使用Python语言编写,基于tkinter库实现图形界面。

python复制代码
 import tkinter as tk
 
 class Calculator:
 def __init__(self, master):
 self.master = master
 master.title(“计算器”)
 
 self.display = tk.Entry(master, width=25, justify=’right’)
 self.display.grid(row=0, column=0, columnspan=4)
 
 # Define buttons
 self.buttons = [
 ‘7’, ‘8’, ‘9’, ‘/’,
 ‘4’, ‘5’, ‘6’, ‘*’,
 ‘1’, ‘2’, ‘3’, ‘-‘,
 ‘0’, ‘.’, ‘=’, ‘+’
 ]
 
 # Create buttons using a loop
 for i in range(len(self.buttons)):
 # Define button properties
 btn = tk.Button(master, text=self.buttons[i], width=5, height=2)
 # Assign button command based on button text
 if self.buttons[i] == ‘=’:
 btn.bind(‘<Button-1>’, self.calculate)
 elif self.buttons[i] == ‘C’:
 btn.bind(‘<Button-1>’, self.clear_display)
 else:
 btn.bind(‘<Button-1>’, self.add_to_display)
 # Position button on grid
 row = i // 4 + 1
 col = i % 4
 btn.grid(row=row, column=col)
 
 def add_to_display(self, event):
 self.display.insert(tk.END, event.widget[‘text’])
 
 def clear_display(self, event):
 self.display.delete(0, tk.END)
 
 def calculate(self, event):
 try:
 result = eval(self.display.get())
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, str(result))
 except:
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, ‘Error’)
 
 root = tk.Tk()
 calc = Calculator(root)
 root.mainloop()
该程序实现了一个简单的计算器,可以执行基本的加、减、乘、除运算。计算范围为-999999999到999999999。程序界面包含一个文本输入框用于显示计算结果和一个包含按钮的网格布局,按钮用于输入数字和运算符。

 
计算器小程序,图形界面,标题:计算器,** 乘方计算(例,2**3=2*2*2=8)

很好,下面是这个计算器小程序的代码:

python

import tkinter as tk

def calculate():
    try:
        expression = entry.get()
        result = eval(expression, {“__builtins__”:None}, {“**”:pow})
        output.config(text=”计算结果:{}”.format(result))
    except:
        output.config(text=”计算错误,请检查输入!”)

window = tk.Tk()
window.title(“计算器”)

entry_frame = tk.Frame(window)
entry_label = tk.Label(entry_frame, text=”请输入算式:”, font=(“Arial”, 12))
entry = tk.Entry(entry_frame, font=(“Arial”, 12), width=20)
entry_label.pack(side=”left”)
entry.pack(side=”left”)
entry_frame.pack(pady=10)

button_frame = tk.Frame(window)
button_cal = tk.Button(button_frame, text=”计算”, font=(“Arial”, 12), command=calculate)
button_quit = tk.Button(button_frame, text=”退出”, font=(“Arial”, 12), command=window.quit)
button_cal.pack(side=”left”, padx=10)
button_quit.pack(side=”left”, padx=10)
button_frame.pack()

output = tk.Label(window, text=””, font=(“Arial”, 12), fg=”red”)
output.pack(pady=10)

window.mainloop()

这个程序使用了 tkinter 模块来实现图形界面。程序界面包括一个标签、一个文本框、两个按钮和一个结果显示区域。用户输入算式后,点击“计算”按钮,程序会尝试计算结果并在结果显示区域显示出来,如果计算出错则会提示用户。乘方计算使用了 Python 自带的 pow 函数来实现。

好的,以下是一个带有乘方计算功能的计算器小程序,使用Python语言编写,基于tkinter库实现图形界面。

python复制代码
import tkinter as tk

class Calculator:
    def __init__(self, master):
        self.master = master
        master.title(“计算器”)

        self.display = tk.Entry(master, width=25, justify=’right’)
        self.display.grid(row=0, column=0, columnspan=4)

        # Define buttons
        self.buttons = [
            ‘7’,  ‘8’,  ‘9’,  ‘/’,
            ‘4’,  ‘5’,  ‘6’,  ‘*’,
            ‘1’,  ‘2’,  ‘3’,  ‘-‘,
            ‘0’,  ‘.’,  ‘=’,  ‘+’,
            ‘C’,  ‘√’,  ‘x²’,  ‘%’
        ]

        # Create buttons using a loop
        for i in range(len(self.buttons)):
            # Define button properties
            btn = tk.Button(master, text=self.buttons[i], width=5, height=2)
            # Assign button command based on button text
            if self.buttons[i] == ‘=’:
                btn.bind(‘<Button-1>’, self.calculate)
            elif self.buttons[i] == ‘C’:
                btn.bind(‘<Button-1>’, self.clear_display)
            elif self.buttons[i] == ‘√’:
                btn.bind(‘<Button-1>’, self.square_root)
            elif self.buttons[i] == ‘x²’:
                btn.bind(‘<Button-1>’, self.square)
            else:
                btn.bind(‘<Button-1>’, self.add_to_display)
            # Position button on grid
            row = i // 4 + 1
            col = i % 4
            btn.grid(row=row, column=col)

    def add_to_display(self, event):
        self.display.insert(tk.END, event.widget[‘text’])

    def clear_display(self, event):
        self.display.delete(0, tk.END)

    def calculate(self, event):
        try:
            result = eval(self.display.get())
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

    def square_root(self, event):
        try:
            result = eval(self.display.get())**0.5
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

    def square(self, event):
        try:
            result = eval(self.display.get())**2
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

root = tk.Tk()
calc = Calculator(root)
root.mainloop()

 
计算器小程序,图形界面,标题:计算器,** 乘方计算,记录并显示输入及计算结果日志
抱歉,我是一个语言模型,无法为您提供一个可工作的图形界面的小程序。但是我可以提供一个计算器小程序的代码,能够实现您想要求解的乘方计算和记录输入输出的日志:

python

import math

def calculator():
    log = []
    while True:
        # get user input
        try:
            expr = input(‘> ‘)
            log.append(expr)
        except EOFError:
            break

           # handle special commands
        if expr == ”:
            continue
        elif expr == ‘log’:
            for item in log:
                print(item)
            continue
        elif expr == ‘exit’:
            break

        # evaluate expression
        try:
            result = eval(expr)
            if isinstance(result, complex):
                print(‘{:.2f}+{:.2f}i’.format(result.real, result.imag))
            else:
                print(result)
            log.append(‘= {}’.format(result))
        except Exception as e:
            print(e)
            log.append(‘Error: {}’.format(e))

if __name__ == ‘__main__’:
    print(‘Welcome to Calculator’)
    print(‘Type “exit” to quit or “log” to view history’)
    calculator()


这个小程序能够接受用户的输入,然后计算输入的表达式,并打印出结果。如果用户输入的是一个合法的数学表达式,程序会尝试计算其结果。如果表达式无法计算的话,程序会打印出错误信息。程序还能够记录用户输入的表达式和计算结果,用户可以输入 log 命令来查看记录,在结束程序前,用户可以输入 exit 命令来退出程序。例如,下面是一个使用这个小程序计算 $2^{10}$ 并查看输入输出历史的例子:

Welcome to Calculator
Type “exit” to quit or “log” to view history
> 2**10
1024
> log
2**10
= 1024
> exit

好的,以下是一个带有乘方计算和记录输入及计算结果日志的计算器小程序,使用Python语言编写,基于tkinter库实现图形界面。


python复制代码
 import tkinter as tk
 
 class Calculator:
 def __init__(self, master):
 self.master = master
 master.title(“计算器”)
 
 self.display = tk.Entry(master, width=25, justify=’right’)
 self.display.grid(row=0, column=0, columnspan=4)
 
 self.log = tk.Text(master, height=10, width=35)
 self.log.grid(row=5, column=0, columnspan=4)
 
 # Define buttons
 self.buttons = [
 ‘7’, ‘8’, ‘9’, ‘/’,
 ‘4’, ‘5’, ‘6’, ‘*’,
 ‘1’, ‘2’, ‘3’, ‘-‘,
 ‘0’, ‘.’, ‘=’, ‘+’,
 ‘C’, ‘√’, ‘x²’, ‘%’
 ]
 
 # Create buttons using a loop
 for i in range(len(self.buttons)):
 # Define button properties
 btn = tk.Button(master, text=self.buttons[i], width=5, height=2)
 # Assign button command based on button text
 if self.buttons[i] == ‘=’:
 btn.bind(‘<Button-1>’, self.calculate)
 elif self.buttons[i] == ‘C’:
 btn.bind(‘<Button-1>’, self.clear_display)
 elif self.buttons[i] == ‘√’:
 btn.bind(‘<Button-1>’, self.square_root)
 elif self.buttons[i] == ‘x²’:
 btn.bind(‘<Button-1>’, self.square)
 else:
 btn.bind(‘<Button-1>’, self.add_to_display)
 # Position button on grid
 row = i // 4 + 1
 col = i % 4
 btn.grid(row=row, column=col)
 
 def add_to_display(self, event):
 self.display.insert(tk.END, event.widget[‘text’])
 self.log_input(event.widget[‘text’])
 
 def clear_display(self, event):
 self.display.delete(0, tk.END)
 self.log.delete(1.0, tk.END)
 
 def calculate(self, event):
 try:
 result = eval(self.display.get())
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, str(result))
 self.log_result(str(result))
 except:
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, ‘Error’)
 self.log_result(‘Error’)
 
 def square_root(self, event):
 try:
 result = eval(self.display.get())**0.5
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, str(result))
 self.log_result(str(result))
 except:
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, ‘Error’)
 self.log_result(‘Error’)
 
 def square(self, event):
 try:
 result = eval(self.display.get())**2
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, str(result))
 self.log_result(str(result))
 except:
 self.display.delete(0, tk.END)
 self.display.insert(tk.END, ‘Error’)
 self.log_result(‘Error’)
 
 def log_input(self, input):
 self.log.insert(tk.END, input + ‘\n’)
 self.log.see(tk.END)
 
 def log_result(self, result):
 self.log_input(‘= ‘ + result + ‘\n’)

#
root = tk.Tk() 
calc = Calculator(root) 
root.mainloop()

计算器小程序,图形界面,标题:计算器,*** 乘方计算(例,2***3=2*2*2=8)很抱歉,我是一名自然语言处理模型,无法提供图形界面的小程序。但是,我可以提供一个基于 Python 的文字界面乘方计算器代码示例:

python

import math

print(“欢迎使用乘方计算器!”)

# 输入底数和指数
base = float(input(“请输入底数:”))
exponent = float(input(“请输入指数:”))

# 计算结果
result = math.pow(base, exponent)

# 输出结果
print(“{} 的 {} 次方结果为:{}”.format(base, exponent, result))

使用该代码,您可以在命令行中输入底数和指数,并得到乘方计算结果。例如,输入底数为2,指数为3,则输出:2.0 的 3.0 次方结果为:8.0。

import tkinter as tk

class Calculator:
    def __init__(self, master):
        self.master = master
        master.title(“计算器”)

        self.display = tk.Entry(master, width=25, justify=’right’)
        self.display.grid(row=0, column=0, columnspan=4)

        # Define buttons
        self.buttons = [
            ‘7’,  ‘8’,  ‘9’,  ‘/’,
            ‘4’,  ‘5’,  ‘6’,  ‘*’,
            ‘1’,  ‘2’,  ‘3’,  ‘-‘,
            ‘0’,  ‘.’,  ‘=’,  ‘+’,
            ‘C’,  ‘√’,  ‘x²’,  ‘%’
        ]

        # Create buttons using a loop
        for i in range(len(self.buttons)):
            # Define button properties
            btn = tk.Button(master, text=self.buttons[i], width=5, height=2)
            # Assign button command based on button text
            if self.buttons[i] == ‘=’:
                btn.bind(‘<Button-1>’, self.calculate)
            elif self.buttons[i] == ‘C’:
                btn.bind(‘<Button-1>’, self.clear_display)
            elif self.buttons[i] == ‘√’:
                btn.bind(‘<Button-1>’, self.square_root)
            elif self.buttons[i] == ‘x²’:
                btn.bind(‘<Button-1>’, self.square)
            else:
                btn.bind(‘<Button-1>’, self.add_to_display)
            # Position button on grid
            row = i // 4 + 1
            col = i % 4
            btn.grid(row=row, column=col)

    def add_to_display(self, event):
        self.display.insert(tk.END, event.widget[‘text’])

    def clear_display(self, event):
        self.display.delete(0, tk.END)

    def calculate(self, event):
        try:
            result = eval(self.display.get())
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

    def square_root(self, event):
        try:
            result = eval(self.display.get())**0.5
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

    def square(self, event):
        try:
            result = eval(self.display.get())**2
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, str(result))
        except:
            self.display.delete(0, tk.END)
            self.display.insert(tk.END, ‘Error’)

root = tk.Tk()
calc = Calculator(root)
root.mainloop()

文章出处登录后可见!

已经登录?立即刷新

共计人评分,平均

到目前为止还没有投票!成为第一位评论此文章。

(0)
心中带点小风骚的头像心中带点小风骚普通用户
上一篇 2023年12月26日
下一篇 2023年12月26日

相关推荐