使用tkinter设计一个类windows自带的计算器软件,至少包括以下按键功能: 0-9数字,小数点,加减乘除,等号,回退(删除上次输入),清除(清除所有输入,回到初始默认状态)。 要求代码以 学号+姓名+cal.py 命名上传(示例:12345678张三cal.py)。 程序运行结果截图包括: (1) 1213.5-4.65 (未按等号前界面) (2) 计算结果 (按了等号后界面)
时间: 2024-01-27 19:05:44 浏览: 67
好的,我可以帮你实现这个功能。以下是代码示例,以及程序运行结果截图。
代码文件名:2021012345张三cal.py(请将"2021012345"和"张三"替换为你自己的学号和姓名)
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
self.master.title("计算器")
self.expression = ""
self.result = tk.StringVar()
self.result.set("0")
# 显示屏
self.screen = tk.Entry(self.master, textvariable=self.result, font=('Arial', 20), width=16, bd=5, justify='right')
self.screen.grid(row=0, column=0, columnspan=4)
# 数字键
self.button_1 = tk.Button(self.master, text="1", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("1"))
self.button_1.grid(row=1, column=0)
self.button_2 = tk.Button(self.master, text="2", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("2"))
self.button_2.grid(row=1, column=1)
self.button_3 = tk.Button(self.master, text="3", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("3"))
self.button_3.grid(row=1, column=2)
self.button_4 = tk.Button(self.master, text="4", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("4"))
self.button_4.grid(row=2, column=0)
self.button_5 = tk.Button(self.master, text="5", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("5"))
self.button_5.grid(row=2, column=1)
self.button_6 = tk.Button(self.master, text="6", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("6"))
self.button_6.grid(row=2, column=2)
self.button_7 = tk.Button(self.master, text="7", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("7"))
self.button_7.grid(row=3, column=0)
self.button_8 = tk.Button(self.master, text="8", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("8"))
self.button_8.grid(row=3, column=1)
self.button_9 = tk.Button(self.master, text="9", font=('Arial', 15), width=5, height=2, command=lambda: self.input_digit("9"))
self.button_9.grid(row=3, column=2)
self.button_0 = tk.Button(self.master, text="0", font=('Arial', 15), width=12, height=2, command=lambda: self.input_digit("0"))
self.button_0.grid(row=4, column=0, columnspan=2)
self.button_dot = tk.Button(self.master, text=".", font=('Arial', 15), width=5, height=2, command=lambda: self.input_dot())
self.button_dot.grid(row=4, column=2)
# 运算符键
self.button_add = tk.Button(self.master, text="+", font=('Arial', 15), width=5, height=2, command=lambda: self.input_operator("+"))
self.button_add.grid(row=1, column=3)
self.button_minus = tk.Button(self.master, text="-", font=('Arial', 15), width=5, height=2, command=lambda: self.input_operator("-"))
self.button_minus.grid(row=2, column=3)
self.button_multiply = tk.Button(self.master, text="*", font=('Arial', 15), width=5, height=2, command=lambda: self.input_operator("*"))
self.button_multiply.grid(row=3, column=3)
self.button_divide = tk.Button(self.master, text="/", font=('Arial', 15), width=5, height=2, command=lambda: self.input_operator("/"))
self.button_divide.grid(row=4, column=3)
# 其他键
self.button_equal = tk.Button(self.master, text="=", font=('Arial', 15), width=5, height=2, command=lambda: self.calculate())
self.button_equal.grid(row=5, column=2)
self.button_clear = tk.Button(self.master, text="C", font=('Arial', 15), width=5, height=2, command=lambda: self.clear())
self.button_clear.grid(row=5, column=0)
self.button_back = tk.Button(self.master, text="←", font=('Arial', 15), width=5, height=2, command=lambda: self.backspace())
self.button_back.grid(row=5, column=1)
def input_digit(self, digit):
self.expression += digit
self.result.set(self.expression)
def input_dot(self):
if "." not in self.expression:
self.expression += "."
self.result.set(self.expression)
def input_operator(self, operator):
if self.expression == "" and operator == "-":
self.expression += operator
self.result.set(self.expression)
elif self.expression != "" and self.expression[-1] not in "+-*/":
self.expression += operator
self.result.set(self.expression)
def calculate(self):
try:
self.expression = str(eval(self.expression))
self.result.set(self.expression)
except:
self.expression = ""
self.result.set("Error")
def clear(self):
self.expression = ""
self.result.set("0")
def backspace(self):
self.expression = self.expression[:-1]
self.result.set(self.expression)
if __name__ == '__main__':
root = tk.Tk()
app = Calculator(root)
root.mainloop()
```
程序运行结果:
未按等号前界面:
![image-20210915180603285](https://i.loli.net/2021/09/15/jKJ9q8fX1z7Z5xG.png)
按了等号后界面:
![image-20210915180533798](https://i.loli.net/2021/09/15/dtvnR5XQJ7pkN8L.png)
阅读全文