用tkinter实现一个简单计算器,支持小数点,多次运算,由label和button构成,grid布局
时间: 2023-07-01 07:06:58 浏览: 140
python使用tkinter实现简单计算器
好的,下面是一个简单的计算器示例代码,使用 tkinter 实现,支持小数点,多次运算:
```python
import tkinter as tk
class Calculator(tk.Frame):
def __init__(self, master=None):
super().__init__(master)
self.master = master
self.master.title("Calculator")
self.pack()
self.create_widgets()
def create_widgets(self):
# 创建显示结果的标签
self.result_label = tk.Label(self, text="0", width=20, height=2, bg="white", font=("Arial", 18))
self.result_label.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
# 创建按钮
self.button_1 = tk.Button(self, text="1", width=5, height=2, command=lambda:self.add_number("1"))
self.button_2 = tk.Button(self, text="2", width=5, height=2, command=lambda:self.add_number("2"))
self.button_3 = tk.Button(self, text="3", width=5, height=2, command=lambda:self.add_number("3"))
self.button_4 = tk.Button(self, text="4", width=5, height=2, command=lambda:self.add_number("4"))
self.button_5 = tk.Button(self, text="5", width=5, height=2, command=lambda:self.add_number("5"))
self.button_6 = tk.Button(self, text="6", width=5, height=2, command=lambda:self.add_number("6"))
self.button_7 = tk.Button(self, text="7", width=5, height=2, command=lambda:self.add_number("7"))
self.button_8 = tk.Button(self, text="8", width=5, height=2, command=lambda:self.add_number("8"))
self.button_9 = tk.Button(self, text="9", width=5, height=2, command=lambda:self.add_number("9"))
self.button_0 = tk.Button(self, text="0", width=5, height=2, command=lambda:self.add_number("0"))
self.button_dot = tk.Button(self, text=".", width=5, height=2, command=lambda:self.add_number("."))
self.button_plus = tk.Button(self, text="+", width=5, height=2, command=lambda:self.add_operator("+"))
self.button_minus = tk.Button(self, text="-", width=5, height=2, command=lambda:self.add_operator("-"))
self.button_multiply = tk.Button(self, text="×", width=5, height=2, command=lambda:self.add_operator("*"))
self.button_divide = tk.Button(self, text="÷", width=5, height=2, command=lambda:self.add_operator("/"))
self.button_clear = tk.Button(self, text="C", width=5, height=2, command=self.clear)
self.button_equal = tk.Button(self, text="=", width=5, height=2, command=self.calculate)
# 按钮布局
self.button_1.grid(row=1, column=0, padx=5, pady=5)
self.button_2.grid(row=1, column=1, padx=5, pady=5)
self.button_3.grid(row=1, column=2, padx=5, pady=5)
self.button_plus.grid(row=1, column=3, 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_minus.grid(row=2, column=3, padx=5, pady=5)
self.button_7.grid(row=3, column=0, padx=5, pady=5)
self.button_8.grid(row=3, column=1, padx=5, pady=5)
self.button_9.grid(row=3, column=2, padx=5, pady=5)
self.button_multiply.grid(row=3, column=3, padx=5, pady=5)
self.button_dot.grid(row=4, column=0, padx=5, pady=5)
self.button_0.grid(row=4, column=1, padx=5, pady=5)
self.button_clear.grid(row=4, column=2, padx=5, pady=5)
self.button_divide.grid(row=4, column=3, padx=5, pady=5)
self.button_equal.grid(row=5, column=0, columnspan=4, padx=5, pady=5)
# 计算器状态变量
self.current_number = "0"
self.operator = None
self.result = None
def add_number(self, number):
# 添加数字到当前数字串
if self.current_number == "0":
self.current_number = number
else:
self.current_number += number
self.result_label.config(text=self.current_number)
def add_operator(self, operator):
# 记录当前运算符,并计算结果
if self.result is None:
self.result = float(self.current_number)
else:
self.calculate()
self.current_number = "0"
self.operator = operator
def clear(self):
# 清空计算器状态
self.current_number = "0"
self.operator = None
self.result = None
self.result_label.config(text=self.current_number)
def calculate(self):
# 计算结果
if self.operator is None:
self.result = float(self.current_number)
elif self.operator == "+":
self.result += float(self.current_number)
elif self.operator == "-":
self.result -= float(self.current_number)
elif self.operator == "*":
self.result *= float(self.current_number)
elif self.operator == "/":
self.result /= float(self.current_number)
self.current_number = str(self.result)
self.operator = None
self.result_label.config(text=self.current_number)
if __name__ == '__main__':
root = tk.Tk()
app = Calculator(master=root)
app.mainloop()
```
运行上述代码,即可看到简单的计算器界面。
阅读全文