用python写代码:用tkinter实现一个简单计算器,可以进行加减乘除四则运算,支持小数点,支持多次运算,由label和button构成,并灵活运用eval函数,用grid布局,并有除零错误提示。(不使用class分类))
时间: 2024-02-18 13:59:42 浏览: 121
好的,以下是代码实现:
```python
import tkinter as tk
def calculate():
try:
result = eval(entry.get())
label.config(text="计算结果:" + str(result))
except ZeroDivisionError:
label.config(text="错误:除数不能为0")
except:
label.config(text="错误:输入有误")
def button_click(number):
current = entry.get()
entry.delete(0, tk.END)
entry.insert(0, str(current) + str(number))
def clear():
entry.delete(0, tk.END)
label.config(text="计算结果:")
# 创建窗口
window = tk.Tk()
window.title("简单计算器")
# 创建输入框和标签
entry = tk.Entry(window, width=30, borderwidth=5)
entry.grid(row=0, column=0, columnspan=4, padx=10, pady=10)
label = tk.Label(window, text="计算结果:")
label.grid(row=1, column=0, columnspan=4)
# 创建按钮
button_1 = tk.Button(window, text="1", padx=40, pady=20, command=lambda: button_click(1))
button_2 = tk.Button(window, text="2", padx=40, pady=20, command=lambda: button_click(2))
button_3 = tk.Button(window, text="3", padx=40, pady=20, command=lambda: button_click(3))
button_4 = tk.Button(window, text="4", padx=40, pady=20, command=lambda: button_click(4))
button_5 = tk.Button(window, text="5", padx=40, pady=20, command=lambda: button_click(5))
button_6 = tk.Button(window, text="6", padx=40, pady=20, command=lambda: button_click(6))
button_7 = tk.Button(window, text="7", padx=40, pady=20, command=lambda: button_click(7))
button_8 = tk.Button(window, text="8", padx=40, pady=20, command=lambda: button_click(8))
button_9 = tk.Button(window, text="9", padx=40, pady=20, command=lambda: button_click(9))
button_0 = tk.Button(window, text="0", padx=40, pady=20, command=lambda: button_click(0))
button_point = tk.Button(window, text=".", padx=41, pady=20, command=lambda: button_click("."))
button_add = tk.Button(window, text="+", padx=39, pady=20, command=lambda: button_click("+"))
button_subtract = tk.Button(window, text="-", padx=41, pady=20, command=lambda: button_click("-"))
button_multiply = tk.Button(window, text="*", padx=40, pady=20, command=lambda: button_click("*"))
button_divide = tk.Button(window, text="/", padx=41, pady=20, command=lambda: button_click("/"))
button_clear = tk.Button(window, text="清除", padx=77, pady=20, command=clear)
button_equal = tk.Button(window, text="=", padx=91, pady=20, command=calculate)
# 将按钮放入窗口
button_1.grid(row=2, column=0)
button_2.grid(row=2, column=1)
button_3.grid(row=2, column=2)
button_4.grid(row=3, column=0)
button_5.grid(row=3, column=1)
button_6.grid(row=3, column=2)
button_7.grid(row=4, column=0)
button_8.grid(row=4, column=1)
button_9.grid(row=4, column=2)
button_0.grid(row=5, column=0)
button_point.grid(row=5, column=1)
button_add.grid(row=2, column=3)
button_subtract.grid(row=3, column=3)
button_multiply.grid(row=4, column=3)
button_divide.grid(row=5, column=3)
button_clear.grid(row=6, column=0, columnspan=2)
button_equal.grid(row=6, column=2, columnspan=2)
# 运行窗口
window.mainloop()
```
这个代码用tkinter实现了一个简单的计算器,支持加减乘除四则运算,支持小数点,支持多次运算,并且有除零错误提示。
阅读全文