帮我做一份PYTHON采用面向对象的思想设计复杂计算器界面,要求界面整洁,
时间: 2023-08-28 20:05:10 浏览: 199
Python开发带图形用户界面的计算器完整项目流程实战_优秀案例实例源代码源码.zip
5星 · 资源好评率100%
好的,我可以为您提供一个面向对象的计算器界面的设计。首先,我们需要定义一个 Calculator 类,它将包含所有的计算器操作和界面。
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("Calculator")
self.master.geometry("300x400")
self.master.resizable(False, False)
# Create input field
self.input_field = tk.Entry(self.master, width=30, font=("Arial", 14))
self.input_field.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# Create buttons
self.button_1 = tk.Button(self.master, text="1", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(1))
self.button_2 = tk.Button(self.master, text="2", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(2))
self.button_3 = tk.Button(self.master, text="3", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(3))
self.button_4 = tk.Button(self.master, text="4", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(4))
self.button_5 = tk.Button(self.master, text="5", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(5))
self.button_6 = tk.Button(self.master, text="6", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(6))
self.button_7 = tk.Button(self.master, text="7", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(7))
self.button_8 = tk.Button(self.master, text="8", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(8))
self.button_9 = tk.Button(self.master, text="9", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(9))
self.button_0 = tk.Button(self.master, text="0", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click(0))
self.button_add = tk.Button(self.master, text="+", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click("+"))
self.button_subtract = tk.Button(self.master, text="-", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click("-"))
self.button_multiply = tk.Button(self.master, text="*", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click("*"))
self.button_divide = tk.Button(self.master, text="/", width=7, height=3, font=("Arial", 12), command=lambda: self.button_click("/"))
self.button_clear = tk.Button(self.master, text="C", width=7, height=3, font=("Arial", 12), command=self.clear)
self.button_equals = tk.Button(self.master, text="=", width=7, height=3, font=("Arial", 12), command=self.equals)
# Position buttons
self.button_clear.grid(row=1, column=0)
self.button_divide.grid(row=1, column=1)
self.button_multiply.grid(row=1, column=2)
self.button_subtract.grid(row=1, column=3)
self.button_7.grid(row=2, column=0)
self.button_8.grid(row=2, column=1)
self.button_9.grid(row=2, column=2)
self.button_add.grid(row=2, column=3)
self.button_4.grid(row=3, column=0)
self.button_5.grid(row=3, column=1)
self.button_6.grid(row=3, column=2)
self.button_1.grid(row=4, column=0)
self.button_2.grid(row=4, column=1)
self.button_3.grid(row=4, column=2)
self.button_equals.grid(row=4, column=3)
self.button_0.grid(row=5, column=0, columnspan=2)
# Initialize calculator state
self.clear()
def button_click(self, number):
current = self.input_field.get()
self.input_field.delete(0, tk.END)
self.input_field.insert(0, str(current) + str(number))
def clear(self):
self.input_field.delete(0, tk.END)
self.operation = None
self.first_number = None
def equals(self):
second_number = float(self.input_field.get())
self.input_field.delete(0, tk.END)
if self.operation == "+":
result = self.first_number + second_number
elif self.operation == "-":
result = self.first_number - second_number
elif self.operation == "*":
result = self.first_number * second_number
elif self.operation == "/":
result = self.first_number / second_number
self.input_field.insert(0, str(result))
self.operation = None
self.first_number = None
def button_add(self):
self.operation = "+"
self.first_number = float(self.input_field.get())
self.input_field.delete(0, tk.END)
def button_subtract(self):
self.operation = "-"
self.first_number = float(self.input_field.get())
self.input_field.delete(0, tk.END)
def button_multiply(self):
self.operation = "*"
self.first_number = float(self.input_field.get())
self.input_field.delete(0, tk.END)
def button_divide(self):
self.operation = "/"
self.first_number = float(self.input_field.get())
self.input_field.delete(0, tk.END)
```
在这个类中,我们首先定义了一个初始化方法来设置计算器的初始状态,包括窗口标题、大小和不可调整大小。我们还创建了文本框和按钮,每个按钮都有一个相应的命令。
我们还定义了一些其他的方法来处理按钮点击事件和计算器操作。例如,button_click() 方法将数字添加到文本框中,clear() 方法将文本框清空并重置计算器状态,equals() 方法执行计算操作并显示结果。
最后,我们将所有的按钮放置在计算器窗口中,使用 grid() 方法来指定它们的位置。这个方法让我们可以指定每个按钮在窗口中的行和列。
这就是一个简单的面向对象的计算器界面的设计。您可以根据自己的需要进行修改和扩展。
阅读全文