写一个带界面的计算器,包含基本的加减乘除、括号用python语言编写
时间: 2024-02-23 19:02:58 浏览: 72
带界面的计算器
好的,Python有很多GUI库可以使用,比如Tkinter、PyQt等。下面是一个使用Tkinter库实现的简单计算器示例代码,可以实现基本的加减乘除和括号运算:
```python
import tkinter as tk
def calculate():
expression = entry.get()
try:
result = eval(expression)
output.config(text=str(result))
except:
output.config(text="Error")
def add_to_expression(char):
entry.insert(tk.END, char)
def clear():
entry.delete(0, tk.END)
output.config(text="")
root = tk.Tk()
root.title("Calculator")
entry = tk.Entry(root, width=40, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
button_1 = tk.Button(root, text="1", padx=40, pady=20, command=lambda: add_to_expression("1"))
button_2 = tk.Button(root, text="2", padx=40, pady=20, command=lambda: add_to_expression("2"))
button_3 = tk.Button(root, text="3", padx=40, pady=20, command=lambda: add_to_expression("3"))
button_4 = tk.Button(root, text="4", padx=40, pady=20, command=lambda: add_to_expression("4"))
button_5 = tk.Button(root, text="5", padx=40, pady=20, command=lambda: add_to_expression("5"))
button_6 = tk.Button(root, text="6", padx=40, pady=20, command=lambda: add_to_expression("6"))
button_7 = tk.Button(root, text="7", padx=40, pady=20, command=lambda: add_to_expression("7"))
button_8 = tk.Button(root, text="8", padx=40, pady=20, command=lambda: add_to_expression("8"))
button_9 = tk.Button(root, text="9", padx=40, pady=20, command=lambda: add_to_expression("9"))
button_0 = tk.Button(root, text="0", padx=40, pady=20, command=lambda: add_to_expression("0"))
button_add = tk.Button(root, text="+", padx=39, pady=20, command=lambda: add_to_expression("+"))
button_subtract = tk.Button(root, text="-", padx=41, pady=20, command=lambda: add_to_expression("-"))
button_multiply = tk.Button(root, text="*", padx=40, pady=20, command=lambda: add_to_expression("*"))
button_divide = tk.Button(root, text="/", padx=41, pady=20, command=lambda: add_to_expression("/"))
button_left_paren = tk.Button(root, text="(", padx=41, pady=20, command=lambda: add_to_expression("("))
button_right_paren = tk.Button(root, text=")", padx=41, pady=20, command=lambda: add_to_expression(")"))
button_clear = tk.Button(root, text="Clear", padx=79, pady=20, command=clear)
button_equals = tk.Button(root, text="=", padx=91, pady=20, command=calculate)
button_1.grid(row=1, column=0)
button_2.grid(row=1, column=1)
button_3.grid(row=1, column=2)
button_4.grid(row=2, column=0)
button_5.grid(row=2, column=1)
button_6.grid(row=2, column=2)
button_7.grid(row=3, column=0)
button_8.grid(row=3, column=1)
button_9.grid(row=3, column=2)
button_0.grid(row=4, column=0)
button_add.grid(row=5, column=0)
button_subtract.grid(row=6, column=0)
button_multiply.grid(row=6, column=1)
button_divide.grid(row=6, column=2)
button_left_paren.grid(row=5, column=1)
button_right_paren.grid(row=5, column=2)
button_clear.grid(row=4, column=1, columnspan=2)
button_equals.grid(row=7, column=0, columnspan=4, padx=10, pady=10)
output = tk.Label(root, text="", width=40, borderwidth=5)
output.grid(row=8, column=0, columnspan=4, padx=10, pady=10)
root.mainloop()
```
这段代码使用了Tkinter库,创建了一个简单的图形用户界面。用户可以通过按钮输入算式,然后计算器会显示计算结果。如果算式有错误,计算器会显示"Error"。
注意:这个计算器还有很多不足之处,比如没有处理除数为零的情况,也没有处理科学计数法、三角函数、对数函数、求幂等高级功能。如果需要实现这些功能,需要对代码进行进一步修改和扩展。
阅读全文