简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: 首先进行操作数文本框检验,检验输入数据是否为纯数字; 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-10 12:18:19 浏览: 39
好的,我会为您解答。以下是一个简单的Python代码实现该计算器功能:
```python
import tkinter as tk
# 创建主窗口
root = tk.Tk()
root.title("简易计算器")
# 定义加法函数
def add():
try:
operand1 = int(entry1.get())
operand2 = int(entry2.get())
result = operand1 + operand2
entry3.delete(0, tk.END)
entry3.insert(0, str(result))
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "请输入数字")
# 定义减法函数
def subtract():
try:
operand1 = int(entry1.get())
operand2 = int(entry2.get())
result = operand1 - operand2
entry3.delete(0, tk.END)
entry3.insert(0, str(result))
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "请输入数字")
# 定义乘法函数
def multiply():
try:
operand1 = int(entry1.get())
operand2 = int(entry2.get())
result = operand1 * operand2
entry3.delete(0, tk.END)
entry3.insert(0, str(result))
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "请输入数字")
# 定义除法函数
def divide():
try:
operand1 = int(entry1.get())
operand2 = int(entry2.get())
result = operand1 / operand2
entry3.delete(0, tk.END)
entry3.insert(0, str(result))
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "请输入数字")
except ZeroDivisionError:
entry3.delete(0, tk.END)
entry3.insert(0, "除数不能为0")
# 创建标签和文本框
label1 = tk.Label(root, text="操作数1")
label1.grid(row=0, column=0)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)
label2 = tk.Label(root, text="操作数2")
label2.grid(row=1, column=0)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)
label3 = tk.Label(root, text="操作结果")
label3.grid(row=2, column=0)
entry3 = tk.Entry(root)
entry3.grid(row=2, column=1)
# 创建按钮
button1 = tk.Button(root, text="加法(+)", command=add)
button1.grid(row=3, column=0)
button2 = tk.Button(root, text="减法(-)", command=subtract)
button2.grid(row=3, column=1)
button3 = tk.Button(root, text="乘法(*)", command=multiply)
button3.grid(row=4, column=0)
button4 = tk.Button(root, text="除法(/)", command=divide)
button4.grid(row=4, column=1)
# 运行主程序
root.mainloop()
```
在代码中,我们使用了Tkinter库来创建窗口界面。首先创建了一个主窗口,并设置了标题为“简易计算器”。然后,我们定义了四个函数add、subtract、multiply和divide,分别对应加、减、乘、除四种运算。这些函数首先从文本框entry1和entry2中获取操作数,并进行相应的运算。最后,将运算结果写入文本框entry3中。
在窗口界面中,我们创建了三个标签和三个文本框,分别用于显示操作数1、操作数2和操作结果。同时,我们还创建了四个按钮,分别对应加、减、乘、除四种运算。当用户单击这些按钮时,会调用相应的函数进行运算,并将结果显示在文本框entry3中。
需要注意的是,我们在每个函数中都使用了try-except语句来捕捉可能出现的异常。如果用户输入的不是数字,我们将在文本框entry3中显示“请输入数字”;如果用户输入的除数为0,我们将在文本框entry3中显示“除数不能为0”。
希望这个示例代码对您有帮助!
相关推荐
![](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)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)