python实现一个有窗口的复杂的计算器
时间: 2024-02-18 11:00:09 浏览: 43
好的,以下是一个基于 Python Tkinter 模块实现的复杂计算器示例:
```python
import tkinter as tk
from tkinter import messagebox
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("计算器")
self.master.geometry("300x400")
self.master.resizable(False, False)
self.result = tk.StringVar()
self.equation = tk.StringVar()
# 显示器
display = tk.Entry(self.master, textvariable=self.result, font=("Arial", 20), bd=0, justify="right")
display.place(x=0, y=0, height=80, width=300)
# 数字按钮
button_1 = tk.Button(self.master, text="1", font=("Arial", 16), bd=0, command=lambda:self.add_number("1"))
button_1.place(x=0, y=80, height=80, width=75)
button_2 = tk.Button(self.master, text="2", font=("Arial", 16), bd=0, command=lambda:self.add_number("2"))
button_2.place(x=75, y=80, height=80, width=75)
button_3 = tk.Button(self.master, text="3", font=("Arial", 16), bd=0, command=lambda:self.add_number("3"))
button_3.place(x=150, y=80, height=80, width=75)
button_4 = tk.Button(self.master, text="4", font=("Arial", 16), bd=0, command=lambda:self.add_number("4"))
button_4.place(x=0, y=160, height=80, width=75)
button_5 = tk.Button(self.master, text="5", font=("Arial", 16), bd=0, command=lambda:self.add_number("5"))
button_5.place(x=75, y=160, height=80, width=75)
button_6 = tk.Button(self.master, text="6", font=("Arial", 16), bd=0, command=lambda:self.add_number("6"))
button_6.place(x=150, y=160, height=80, width=75)
button_7 = tk.Button(self.master, text="7", font=("Arial", 16), bd=0, command=lambda:self.add_number("7"))
button_7.place(x=0, y=240, height=80, width=75)
button_8 = tk.Button(self.master, text="8", font=("Arial", 16), bd=0, command=lambda:self.add_number("8"))
button_8.place(x=75, y=240, height=80, width=75)
button_9 = tk.Button(self.master, text="9", font=("Arial", 16), bd=0, command=lambda:self.add_number("9"))
button_9.place(x=150, y=240, height=80, width=75)
button_0 = tk.Button(self.master, text="0", font=("Arial", 16), bd=0, command=lambda:self.add_number("0"))
button_0.place(x=75, y=320, height=80, width=75)
# 运算符按钮
button_add = tk.Button(self.master, text="+", font=("Arial", 16), bd=0, command=lambda:self.add_operator("+"))
button_add.place(x=225, y=80, height=80, width=75)
button_subtract = tk.Button(self.master, text="-", font=("Arial", 16), bd=0, command=lambda:self.add_operator("-"))
button_subtract.place(x=225, y=160, height=80, width=75)
button_multiply = tk.Button(self.master, text="×", font=("Arial", 16), bd=0, command=lambda:self.add_operator("*"))
button_multiply.place(x=225, y=240, height=80, width=75)
button_divide = tk.Button(self.master, text="÷", font=("Arial", 16), bd=0, command=lambda:self.add_operator("/"))
button_divide.place(x=225, y=320, height=80, width=75)
# 功能按钮
button_clear = tk.Button(self.master, text="清除", font=("Arial", 16), bd=0, command=self.clear)
button_clear.place(x=0, y=320, height=80, width=75)
button_equal = tk.Button(self.master, text="=", font=("Arial", 16), bd=0, command=self.calculate)
button_equal.place(x=150, y=320, height=80, width=75)
def add_number(self, number):
self.equation.set(self.equation.get() + str(number))
def add_operator(self, operator):
self.equation.set(self.equation.get() + " " + operator + " ")
def clear(self):
self.equation.set("")
self.result.set("")
def calculate(self):
try:
result = eval(self.equation.get())
self.result.set(result)
except:
messagebox.showerror("错误", "表达式输入有误,请重新输入。")
self.clear()
if __name__ == "__main__":
root = tk.Tk()
app = Calculator(root)
root.mainloop()
```
该计算器使用了 Tkinter 模块创建 GUI 窗口,支持数字、基本运算符、清除和等于等功能。用户输入表达式后,单击等于按钮即可计算结果。如果表达式输入有误,计算器会提示错误信息并清空表达式。
阅读全文