📜  如何在python中制作一个复杂的计算器(1)

📅  最后修改于: 2023-12-03 14:52:49.277000             🧑  作者: Mango

如何在 Python 中制作一个复杂的计算器

在 Python 中可以使用 Tkinter 库来制作 GUI 应用程序,并使用 eval 函数来计算表达式。本文将简要概括如何制作一个复杂的计算器。

步骤 1:导入库并创建应用程序
from tkinter import *
root = Tk()
root.title("Calculator")

我们创建了一个名为 "Calculator" 的应用程序窗口。

步骤 2:创建显示器
display = Entry(root, width=35, borderwidth=5)
display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

我们通过 Entry 组件创建了一个文本框,用于显示计算结果。

步骤 3:创建按钮并定义按钮点击函数
buttons = [
    {"text": "1", "row": 1, "column": 0},
    {"text": "2", "row": 1, "column": 1},
    {"text": "3", "row": 1, "column": 2},
    {"text": "4", "row": 2, "column": 0},
    {"text": "5", "row": 2, "column": 1},
    {"text": "6", "row": 2, "column": 2},
    {"text": "7", "row": 3, "column": 0},
    {"text": "8", "row": 3, "column": 1},
    {"text": "9", "row": 3, "column": 2},
    {"text": "0", "row": 4, "column": 1},
    {"text": ".", "row": 4, "column": 0},
    {"text": "=", "row": 4, "column": 2},
    {"text": "+", "row": 1, "column": 3},
    {"text": "-", "row": 2, "column": 3},
    {"text": "*", "row": 3, "column": 3},
    {"text": "/", "row": 4, "column": 3},
    {"text": "C", "row": 0, "column": 3},
]

def button_click(number):
    current = display.get()
    display.delete(0, END)
    display.insert(0, str(current) + str(number))

def button_clear():
    display.delete(0, END)

def button_equal():
    second_number = display.get()
    display.delete(0, END)
    try:
        result = eval(str(first_number) + operator + str(second_number))
        display.insert(0, result)
    except ZeroDivisionError:
        display.insert(0, "ERROR: Cannot divide by zero.")
    except:
        display.insert(0, "ERROR: Invalid expression.")

def button_add():
    global first_number, operator
    operator = "+"
    first_number = display.get()
    display.delete(0, END)

def button_subtract():
    global first_number, operator
    operator = "-"
    first_number = display.get()
    display.delete(0, END)

def button_multiply():
    global first_number, operator
    operator = "*"
    first_number = display.get()
    display.delete(0, END)

def button_divide():
    global first_number, operator
    operator = "/"
    first_number = display.get()
    display.delete(0, END)

for button in buttons:
    button_text = button["text"]
    button_row = button["row"]
    button_column = button["column"]
    button = Button(root, text=button_text, padx=40, pady=20, command=lambda x=button_text: button_click(x))
    button.grid(row=button_row, column=button_column)

button_clear = Button(root, text="Clear", padx=30, pady=20, command=button_clear)
button_clear.grid(row=0, column=2)

button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)

button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=6, column=0)

button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=6, column=1)

button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=6, column=2)

button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=5, column=1, columnspan=2)

我们定义了多个按钮点击函数,用于响应不同的按钮点击事件。此外,我们还创建了各种按钮,并将它们添加到应用程序窗口。

步骤 4:运行应用程序
root.mainloop()

我们通过调用主循环函数 mainloop 来运行应用程序。

这就是制作复杂计算器的所有步骤,下面是完整代码片段:

from tkinter import *
root = Tk()
root.title("Calculator")

display = Entry(root, width=35, borderwidth=5)
display.grid(row=0, column=0, columnspan=4, padx=10, pady=10)

buttons = [
    {"text": "1", "row": 1, "column": 0},
    {"text": "2", "row": 1, "column": 1},
    {"text": "3", "row": 1, "column": 2},
    {"text": "4", "row": 2, "column": 0},
    {"text": "5", "row": 2, "column": 1},
    {"text": "6", "row": 2, "column": 2},
    {"text": "7", "row": 3, "column": 0},
    {"text": "8", "row": 3, "column": 1},
    {"text": "9", "row": 3, "column": 2},
    {"text": "0", "row": 4, "column": 1},
    {"text": ".", "row": 4, "column": 0},
    {"text": "=", "row": 4, "column": 2},
    {"text": "+", "row": 1, "column": 3},
    {"text": "-", "row": 2, "column": 3},
    {"text": "*", "row": 3, "column": 3},
    {"text": "/", "row": 4, "column": 3},
    {"text": "C", "row": 0, "column": 3},
]

def button_click(number):
    current = display.get()
    display.delete(0, END)
    display.insert(0, str(current) + str(number))

def button_clear():
    display.delete(0, END)

def button_equal():
    second_number = display.get()
    display.delete(0, END)
    try:
        result = eval(str(first_number) + operator + str(second_number))
        display.insert(0, result)
    except ZeroDivisionError:
        display.insert(0, "ERROR: Cannot divide by zero.")
    except:
        display.insert(0, "ERROR: Invalid expression.")

def button_add():
    global first_number, operator
    operator = "+"
    first_number = display.get()
    display.delete(0, END)

def button_subtract():
    global first_number, operator
    operator = "-"
    first_number = display.get()
    display.delete(0, END)

def button_multiply():
    global first_number, operator
    operator = "*"
    first_number = display.get()
    display.delete(0, END)

def button_divide():
    global first_number, operator
    operator = "/"
    first_number = display.get()
    display.delete(0, END)

for button in buttons:
    button_text = button["text"]
    button_row = button["row"]
    button_column = button["column"]
    button = Button(root, text=button_text, padx=40, pady=20, command=lambda x=button_text: button_click(x))
    button.grid(row=button_row, column=button_column)

button_clear = Button(root, text="Clear", padx=30, pady=20, command=button_clear)
button_clear.grid(row=0, column=2)

button_add = Button(root, text="+", padx=39, pady=20, command=button_add)
button_add.grid(row=5, column=0)

button_subtract = Button(root, text="-", padx=41, pady=20, command=button_subtract)
button_subtract.grid(row=6, column=0)

button_multiply = Button(root, text="*", padx=40, pady=20, command=button_multiply)
button_multiply.grid(row=6, column=1)

button_divide = Button(root, text="/", padx=41, pady=20, command=button_divide)
button_divide.grid(row=6, column=2)

button_equal = Button(root, text="=", padx=91, pady=20, command=button_equal)
button_equal.grid(row=5, column=1, columnspan=2)

root.mainloop()

现在您就可以运行此代码,并利用 Tkinter 库的 GUI 功能使用自己制作的一个计算器啦!