用tkinter实现一个简单计算器,可以进行加减乘除,支持小数点,多次运算,由lable和button构成,用grid布局
时间: 2024-03-22 20:37:11 浏览: 88
简单计算器,能够实现加减乘除,还有就是开方,对数……
好的,以下是一个简单的计算器示例代码:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
master.resizable(False, False)
self.result_var = tk.StringVar()
result_label = tk.Label(master, textvariable=self.result_var, width=20, anchor="e", bg="white")
result_label.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
button_clear = tk.Button(master, text="C", width=5, height=2, command=self.clear)
button_clear.grid(row=1, column=0)
button_divide = tk.Button(master, text="/", width=5, height=2, command=lambda: self.operation("/"))
button_divide.grid(row=1, column=1)
button_multiply = tk.Button(master, text="*", width=5, height=2, command=lambda: self.operation("*"))
button_multiply.grid(row=1, column=2)
button_minus = tk.Button(master, text="-", width=5, height=2, command=lambda: self.operation("-"))
button_minus.grid(row=1, column=3)
button_7 = tk.Button(master, text="7", width=5, height=2, command=lambda: self.number(7))
button_7.grid(row=2, column=0)
button_8 = tk.Button(master, text="8", width=5, height=2, command=lambda: self.number(8))
button_8.grid(row=2, column=1)
button_9 = tk.Button(master, text="9", width=5, height=2, command=lambda: self.number(9))
button_9.grid(row=2, column=2)
button_plus = tk.Button(master, text="+", width=5, height=2, command=lambda: self.operation("+"))
button_plus.grid(row=2, column=3)
button_4 = tk.Button(master, text="4", width=5, height=2, command=lambda: self.number(4))
button_4.grid(row=3, column=0)
button_5 = tk.Button(master, text="5", width=5, height=2, command=lambda: self.number(5))
button_5.grid(row=3, column=1)
button_6 = tk.Button(master, text="6", width=5, height=2, command=lambda: self.number(6))
button_6.grid(row=3, column=2)
button_equal = tk.Button(master, text="=", width=5, height=2, command=self.calculate)
button_equal.grid(row=3, column=3)
button_1 = tk.Button(master, text="1", width=5, height=2, command=lambda: self.number(1))
button_1.grid(row=4, column=0)
button_2 = tk.Button(master, text="2", width=5, height=2, command=lambda: self.number(2))
button_2.grid(row=4, column=1)
button_3 = tk.Button(master, text="3", width=5, height=2, command=lambda: self.number(3))
button_3.grid(row=4, column=2)
button_decimal = tk.Button(master, text=".", width=5, height=2, command=lambda: self.number("."))
button_decimal.grid(row=5, column=0)
button_0 = tk.Button(master, text="0", width=5, height=2, command=lambda: self.number(0))
button_0.grid(row=5, column=1)
button_exit = tk.Button(master, text="Exit", width=5, height=2, command=master.quit)
button_exit.grid(row=5, column=2)
button_plus_minus = tk.Button(master, text="+/-", width=5, height=2, command=self.plus_minus)
button_plus_minus.grid(row=5, column=3)
self.reset()
def reset(self):
self.first_number = None
self.second_number = None
self.operation_type = None
self.result_var.set("0")
self.is_decimal = False
def clear(self):
self.reset()
def number(self, num):
if num == ".":
if self.is_decimal:
return
self.is_decimal = True
if self.operation_type is None:
if self.first_number is None:
self.first_number = str(num)
else:
self.first_number += str(num)
self.result_var.set(self.first_number)
else:
if self.second_number is None:
self.second_number = str(num)
else:
self.second_number += str(num)
self.result_var.set(self.second_number)
def operation(self, op):
self.operation_type = op
self.is_decimal = False
def plus_minus(self):
if self.operation_type is None:
self.first_number = str(-float(self.first_number))
self.result_var.set(self.first_number)
else:
self.second_number = str(-float(self.second_number))
self.result_var.set(self.second_number)
def calculate(self):
if self.first_number is None or self.second_number is None or self.operation_type is None:
return
if self.operation_type == "+":
result = float(self.first_number) + float(self.second_number)
elif self.operation_type == "-":
result = float(self.first_number) - float(self.second_number)
elif self.operation_type == "*":
result = float(self.first_number) * float(self.second_number)
elif self.operation_type == "/":
result = float(self.first_number) / float(self.second_number)
self.result_var.set(str(result))
self.reset()
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
运行上述代码,将会弹出一个简单的计算器界面。
阅读全文