用python写一个计算器包含数字按钮和符号按钮
时间: 2024-02-09 08:12:02 浏览: 28
好的,以下是一个使用 `tkinter` 模块实现的计算器,包含数字按钮和符号按钮:
```python
import tkinter as tk
# 定义变量
num1 = ""
num2 = ""
operator = ""
# 定义数字按钮点击事件处理函数
def click_num(num):
global num1, num2, operator
if operator == "":
num1 += num
label_result.config(text=num1)
else:
num2 += num
label_result.config(text=num2)
# 定义符号按钮点击事件处理函数
def click_operator(op):
global num1, num2, operator
if num1 != "" and num2 != "":
if operator == "+":
result = float(num1) + float(num2)
elif operator == "-":
result = float(num1) - float(num2)
elif operator == "*":
result = float(num1) * float(num2)
elif operator == "/":
result = float(num1) / float(num2)
label_result.config(text=str(result))
num1 = str(result)
num2 = ""
operator = op
elif num1 != "":
operator = op
# 定义清除按钮点击事件处理函数
def click_clear():
global num1, num2, operator
num1 = ""
num2 = ""
operator = ""
label_result.config(text="")
# 创建窗口
window = tk.Tk()
window.title("计算器")
# 创建结果标签
label_result = tk.Label(window, text="")
label_result.grid(row=0, column=0, columnspan=4, padx=5, pady=5)
# 创建数字按钮
button_1 = tk.Button(window, text="1", width=5, height=2, command=lambda: click_num("1"))
button_1.grid(row=1, column=0, padx=5, pady=5)
button_2 = tk.Button(window, text="2", width=5, height=2, command=lambda: click_num("2"))
button_2.grid(row=1, column=1, padx=5, pady=5)
button_3 = tk.Button(window, text="3", width=5, height=2, command=lambda: click_num("3"))
button_3.grid(row=1, column=2, padx=5, pady=5)
button_4 = tk.Button(window, text="4", width=5, height=2, command=lambda: click_num("4"))
button_4.grid(row=2, column=0, padx=5, pady=5)
button_5 = tk.Button(window, text="5", width=5, height=2, command=lambda: click_num("5"))
button_5.grid(row=2, column=1, padx=5, pady=5)
button_6 = tk.Button(window, text="6", width=5, height=2, command=lambda: click_num("6"))
button_6.grid(row=2, column=2, padx=5, pady=5)
button_7 = tk.Button(window, text="7", width=5, height=2, command=lambda: click_num("7"))
button_7.grid(row=3, column=0, padx=5, pady=5)
button_8 = tk.Button(window, text="8", width=5, height=2, command=lambda: click_num("8"))
button_8.grid(row=3, column=1, padx=5, pady=5)
button_9 = tk.Button(window, text="9", width=5, height=2, command=lambda: click_num("9"))
button_9.grid(row=3, column=2, padx=5, pady=5)
button_0 = tk.Button(window, text="0", width=5, height=2, command=lambda: click_num("0"))
button_0.grid(row=4, column=1, padx=5, pady=5)
# 创建符号按钮
button_add = tk.Button(window, text="+", width=5, height=2, command=lambda: click_operator("+"))
button_add.grid(row=1, column=3, padx=5, pady=5)
button_subtract = tk.Button(window, text="-", width=5, height=2, command=lambda: click_operator("-"))
button_subtract.grid(row=2, column=3, padx=5, pady=5)
button_multiply = tk.Button(window, text="*", width=5, height=2, command=lambda: click_operator("*"))
button_multiply.grid(row=3, column=3, padx=5, pady=5)
button_divide = tk.Button(window, text="/", width=5, height=2, command=lambda: click_operator("/"))
button_divide.grid(row=4, column=3, padx=5, pady=5)
button_clear = tk.Button(window, text="清除", width=5, height=2, command=click_clear)
button_clear.grid(row=4, column=2, padx=5, pady=5)
# 运行窗口
window.mainloop()
```
你可以将上述代码保存为 `calculator_with_buttons.py` 文件,然后在命令行中运行它,就可以打开一个带有数字按钮和符号按钮的计算器。
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)