帮我做一个PYTHON采用面向对象的思想设计复杂计算器界面,要求界面美观,程序结构清晰
时间: 2024-01-05 09:04:45 浏览: 89
界面美观的计算器
好的,我可以为您提供一个更加美观、结构更清晰的代码框架。这个计算器可以进行加、减、乘、除等基本的数学运算,还具有取反、清除、求平方根、求倒数等功能。您可以根据自己的需要进行修改和扩展。
```python
import tkinter as tk
from tkinter import ttk
from math import sqrt
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
# 创建显示器
self.display = tk.Entry(master, width=30, font=("Arial", 20), justify="right")
self.display.grid(row=0, column=0, columnspan=4, padx=10, pady=20)
# 创建数字按钮
buttons = ["7", "8", "9", "4", "5", "6", "1", "2", "3", "0", ".", "+/-"]
for i, symbol in enumerate(buttons):
button = tk.Button(master, text=symbol, font=("Arial", 14), width=5, height=2, command=lambda symbol=symbol: self.button_click(symbol))
button.grid(row=1 + i // 3, column=i % 3, padx=5, pady=5)
# 创建运算符按钮
buttons = ["+", "-", "*", "/", "=", "C", "√", "1/x"]
for i, symbol in enumerate(buttons):
button = tk.Button(master, text=symbol, font=("Arial", 14), width=5, height=2, command=lambda symbol=symbol: self.button_click(symbol))
button.grid(row=1 + i, column=3, padx=5, pady=5)
# 初始化状态
self.reset()
def reset(self):
self.current_value = 0
self.operator = None
self.need_clear = False
def button_click(self, symbol):
if symbol.isdigit() or symbol == ".":
if self.need_clear:
self.display.delete(0, tk.END)
self.need_clear = False
if symbol == "." and "." in self.display.get():
return
self.display.insert(tk.END, symbol)
elif symbol == "+/-":
if self.need_clear:
return
value = float(self.display.get())
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(-value))
elif symbol in ["+", "-", "*", "/"]:
if self.need_clear:
self.operator = symbol
return
self.operator = symbol
self.current_value = float(self.display.get())
self.need_clear = True
elif symbol == "=":
if self.need_clear:
return
if self.operator == "+":
result = self.current_value + float(self.display.get())
elif self.operator == "-":
result = self.current_value - float(self.display.get())
elif self.operator == "*":
result = self.current_value * float(self.display.get())
elif self.operator == "/":
result = self.current_value / float(self.display.get())
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
self.reset()
elif symbol == "C":
self.display.delete(0, tk.END)
self.reset()
elif symbol == "√":
if self.need_clear:
return
value = float(self.display.get())
result = sqrt(value)
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
self.reset()
elif symbol == "1/x":
if self.need_clear:
return
value = float(self.display.get())
if value == 0:
self.display.delete(0, tk.END)
self.display.insert(tk.END, "Math Error")
self.reset()
else:
result = 1 / value
self.display.delete(0, tk.END)
self.display.insert(tk.END, str(result))
self.reset()
root = tk.Tk()
style = ttk.Style(root)
style.configure("TButton", font=("Arial", 14))
calculator = Calculator(root)
root.mainloop()
```
这个代码使用了Tkinter库和ttk库来创建图形用户界面,并使用面向对象的思想将计算器的各个组件封装成Calculator类。在这个类中定义了各种方法来响应用户的输入和计算结果。按钮的样式也使用了ttk库来设置。这个计算器界面更加美观,程序结构更清晰,代码也更加易于修改和扩展。
阅读全文