给下面的代码加上计算器窗口界面,要有显示器和键盘,并分析出生成后的代码信息处理流程: import math class Calculator: def init(self): self.result = 0 def add(self, num): self.result += num def sub(self, num): self.result -= num def mul(self, num): self.result *= num def div(self, num): if num == 0: print("Error: Cannot divide by zero!") else: self.result /= num def getResult(self): return self.result def clear(self): self.result = 0 def main(): calculator = Calculator() while True: print("Please select an operation:") print("1. Add") print("2. Subtract") print("3. Multiply") print("4. Divide") print("5. Clear") print("0. Exit") choice = int(input("Enter your choice: ")) if choice == 0: break elif choice == 1: num = float(input("Enter a number: ")) calculator.add(num) print("Result:", calculator.getResult()) elif choice == 2: num = float(input("Enter a number: ")) calculator.sub(num) print("Result:", calculator.getResult()) elif choice == 3: num = float(input("Enter a number: ")) calculator.mul(num) print("Result:", calculator.getResult()) elif choice == 4: num = float(input("Enter a number: ")) calculator.div(num) print("Result:", calculator.getResult()) elif choice == 5: calculator.clear() print("Result:", calculator.getResult()) else: print("Invalid choice. Please try again.") if name == 'main': main()
时间: 2024-03-19 17:46:20 浏览: 116
java实现计算器图形界面源代码(包含详细注释说明)
以下是加上计算器窗口界面后的代码,包括显示器和键盘界面的设计:
```python
import math
import tkinter as tk
class Calculator:
def __init__(self):
self.result = 0
def add(self, num):
self.result += num
def sub(self, num):
self.result -= num
def mul(self, num):
self.result *= num
def div(self, num):
if num == 0:
self.result = "Error: Cannot divide by zero!"
else:
self.result /= num
def getResult(self):
return self.result
def clear(self):
self.result = 0
class CalculatorGUI:
def __init__(self, master):
self.master = master
master.title("Calculator")
self.result_label = tk.Label(master, text="0", width=20, font=("Arial", 20))
self.result_label.grid(row=0, column=0, columnspan=4)
self.create_buttons()
self.calculator = Calculator()
def create_buttons(self):
self.button1 = tk.Button(self.master, text="1", width=5, height=2, command=lambda: self.button_click(1))
self.button2 = tk.Button(self.master, text="2", width=5, height=2, command=lambda: self.button_click(2))
self.button3 = tk.Button(self.master, text="3", width=5, height=2, command=lambda: self.button_click(3))
self.button4 = tk.Button(self.master, text="4", width=5, height=2, command=lambda: self.button_click(4))
self.button5 = tk.Button(self.master, text="5", width=5, height=2, command=lambda: self.button_click(5))
self.button6 = tk.Button(self.master, text="6", width=5, height=2, command=lambda: self.button_click(6))
self.button7 = tk.Button(self.master, text="7", width=5, height=2, command=lambda: self.button_click(7))
self.button8 = tk.Button(self.master, text="8", width=5, height=2, command=lambda: self.button_click(8))
self.button9 = tk.Button(self.master, text="9", width=5, height=2, command=lambda: self.button_click(9))
self.button0 = tk.Button(self.master, text="0", width=5, height=2, command=lambda: self.button_click(0))
self.button_add = tk.Button(self.master, text="+", width=5, height=2, command=lambda: self.operation_click("+"))
self.button_subtract = tk.Button(self.master, text="-", width=5, height=2, command=lambda: self.operation_click("-"))
self.button_multiply = tk.Button(self.master, text="*", width=5, height=2, command=lambda: self.operation_click("*"))
self.button_divide = tk.Button(self.master, text="/", width=5, height=2, command=lambda: self.operation_click("/"))
self.button_clear = tk.Button(self.master, text="C", width=5, height=2, command=lambda: self.clear_click())
self.button_equals = tk.Button(self.master, text="=", width=5, height=2, command=lambda: self.equals_click())
self.button1.grid(row=3, column=0)
self.button2.grid(row=3, column=1)
self.button3.grid(row=3, column=2)
self.button_add.grid(row=3, column=3)
self.button4.grid(row=2, column=0)
self.button5.grid(row=2, column=1)
self.button6.grid(row=2, column=2)
self.button_subtract.grid(row=2, column=3)
self.button7.grid(row=1, column=0)
self.button8.grid(row=1, column=1)
self.button9.grid(row=1, column=2)
self.button_multiply.grid(row=1, column=3)
self.button_clear.grid(row=4, column=0)
self.button0.grid(row=4, column=1)
self.button_equals.grid(row=4, column=2)
self.button_divide.grid(row=4, column=3)
def button_click(self, number):
current = self.result_label.cget("text")
if current == "0":
self.result_label.config(text=str(number))
else:
self.result_label.config(text=current + str(number))
def operation_click(self, operation):
current = self.result_label.cget("text")
self.calculator.result = float(current)
self.operation = operation
self.result_label.config(text="0")
def clear_click(self):
self.calculator.clear()
self.result_label.config(text="0")
def equals_click(self):
current = self.result_label.cget("text")
self.calculator.result = float(current)
if self.operation == "+":
self.calculator.add(self.calculator.getResult())
elif self.operation == "-":
self.calculator.sub(self.calculator.getResult())
elif self.operation == "*":
self.calculator.mul(self.calculator.getResult())
elif self.operation == "/":
self.calculator.div(self.calculator.getResult())
self.result_label.config(text=str(self.calculator.getResult()))
if __name__ == '__main__':
root = tk.Tk()
calculator_gui = CalculatorGUI(root)
root.mainloop()
```
这个代码为用户提供了一个计算器界面,包括数字键、运算符键、清除键、等于键和显示器。当用户点击数字键时,数字将添加到显示器上。当用户点击运算符键时,当前数字将被存储并清除显示器。当用户点击等于键时,计算器将执行相应的操作并将结果显示在显示器上。如果用户点击清除键,那么计算器的结果将被重置为零。整个程序的控制流程是由按钮的事件处理程序控制的,这些事件处理程序与 Calculator 类中定义的方法相对应。
阅读全文