使用tkinter设计一个类windows自带的计算器软件,至少包括以下按键功能: 0-9数字,小数点,加减乘除,等号,回退(删除上次输入),清除(清除所有输入,回到初始默认状态)。 要求代码以 学号+姓名+cal.py 命名上传(示例:12345678张三cal.py)。 程序运行结果截图包括: (1) 12*13.5-4.6*5 (未按等号前界面) (2) 计算结果 (按了等号后界面),并对代码做逐行注释
时间: 2024-01-28 13:04:55 浏览: 150
【案例】用 tkinter 做一个计算器吧?(版本二)
好的,下面是一份实现上述要求的 Python 代码,我将为您逐行注释:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("Calculator") # 设置窗口标题
self.master.geometry("300x330") # 设置窗口大小
self.master.resizable(False, False) # 禁止调整窗口大小
# 定义显示屏
self.screen = tk.Entry(self.master, bd=5, font=("Helvetica", 20), justify=tk.RIGHT)
self.screen.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 定义数字按钮
self.button_0 = tk.Button(self.master, text="0", width=5, height=2, command=lambda: self.button_click("0"))
self.button_1 = tk.Button(self.master, text="1", width=5, height=2, command=lambda: self.button_click("1"))
self.button_2 = tk.Button(self.master, text="2", width=5, height=2, command=lambda: self.button_click("2"))
self.button_3 = tk.Button(self.master, text="3", width=5, height=2, command=lambda: self.button_click("3"))
self.button_4 = tk.Button(self.master, text="4", width=5, height=2, command=lambda: self.button_click("4"))
self.button_5 = tk.Button(self.master, text="5", width=5, height=2, command=lambda: self.button_click("5"))
self.button_6 = tk.Button(self.master, text="6", width=5, height=2, command=lambda: self.button_click("6"))
self.button_7 = tk.Button(self.master, text="7", width=5, height=2, command=lambda: self.button_click("7"))
self.button_8 = tk.Button(self.master, text="8", width=5, height=2, command=lambda: self.button_click("8"))
self.button_9 = tk.Button(self.master, text="9", width=5, height=2, command=lambda: self.button_click("9"))
self.button_point = tk.Button(self.master, text=".", width=5, height=2, command=lambda: self.button_click("."))
# 将数字按钮添加到窗口中
self.button_0.grid(row=4, column=0, padx=5, pady=5)
self.button_1.grid(row=3, column=0, padx=5, pady=5)
self.button_2.grid(row=3, column=1, padx=5, pady=5)
self.button_3.grid(row=3, column=2, padx=5, pady=5)
self.button_4.grid(row=2, column=0, padx=5, pady=5)
self.button_5.grid(row=2, column=1, padx=5, pady=5)
self.button_6.grid(row=2, column=2, padx=5, pady=5)
self.button_7.grid(row=1, column=0, padx=5, pady=5)
self.button_8.grid(row=1, column=1, padx=5, pady=5)
self.button_9.grid(row=1, column=2, padx=5, pady=5)
self.button_point.grid(row=4, column=1, padx=5, pady=5)
# 定义操作符按钮
self.button_add = tk.Button(self.master, text="+", width=5, height=2, command=lambda: self.operation("+"))
self.button_minus = tk.Button(self.master, text="-", width=5, height=2, command=lambda: self.operation("-"))
self.button_mult = tk.Button(self.master, text="*", width=5, height=2, command=lambda: self.operation("*"))
self.button_divide = tk.Button(self.master, text="/", width=5, height=2, command=lambda: self.operation("/"))
self.button_equal = tk.Button(self.master, text="=", width=5, height=2, command=lambda: self.calculate())
self.button_clear = tk.Button(self.master, text="C", width=5, height=2, command=lambda: self.clear())
self.button_back = tk.Button(self.master, text="<-", width=5, height=2, command=lambda: self.backspace())
# 将操作符按钮添加到窗口中
self.button_add.grid(row=1, column=3, padx=5, pady=5)
self.button_minus.grid(row=2, column=3, padx=5, pady=5)
self.button_mult.grid(row=3, column=3, padx=5, pady=5)
self.button_divide.grid(row=4, column=3, padx=5, pady=5)
self.button_equal.grid(row=4, column=2, padx=5, pady=5)
self.button_clear.grid(row=4, column=4, padx=5, pady=5)
self.button_back.grid(row=1, column=4, padx=5, pady=5)
# 初始化计算器状态
self.reset()
def reset(self):
self.current_number = "" # 当前输入的数字
self.result = 0 # 当前计算结果
self.operator = "" # 当前操作符
self.screen.delete(0, tk.END) # 清空显示屏
def button_click(self, number):
self.current_number += number # 将数字添加到当前输入的数字中
self.screen.delete(0, tk.END) # 清空显示屏
self.screen.insert(0, self.current_number) # 将当前输入的数字显示到屏幕上
def operation(self, operator):
if self.current_number: # 如果当前输入的数字不为空
if self.operator: # 如果当前已经有一个操作符
self.calculate() # 先执行计算
self.result = float(self.current_number) # 将当前输入的数字转换为浮点数,并赋值给计算结果
self.operator = operator # 更新当前操作符
self.current_number = "" # 清空当前输入的数字
def calculate(self):
if self.current_number: # 如果当前输入的数字不为空
number = float(self.current_number) # 将当前输入的数字转换为浮点数
if self.operator == "+": # 加法
self.result += number
elif self.operator == "-": # 减法
self.result -= number
elif self.operator == "*": # 乘法
self.result *= number
elif self.operator == "/": # 除法
self.result /= number
self.screen.delete(0, tk.END) # 清空显示屏
self.screen.insert(0, str(self.result)) # 将计算结果显示到屏幕上
self.operator = "" # 清空当前操作符
self.current_number = "" # 清空当前输入的数字
def clear(self):
self.reset() # 重置计算器状态
def backspace(self):
if self.current_number: # 如果当前输入的数字不为空
self.current_number = self.current_number[:-1] # 删除最后一个字符
self.screen.delete(0, tk.END) # 清空显示屏
self.screen.insert(0, self.current_number) # 将当前输入的数字显示到屏幕上
root = tk.Tk()
app = Calculator(root)
root.mainloop()
```
这是一个使用 Python 的 tkinter 库实现的简单计算器。可以通过运行该程序在窗口中进行数学运算。
阅读全文