简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: 首先进行操作数文本框检验,检验输入数据是否为纯数字; 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-10 17:20:12 浏览: 63
好的,这是一个简单的计算器程序,我会用Python语言为您编写。首先,我们需要使用tkinter模块创建一个窗口,并在窗口中添加文本框和按钮。然后,我们需要定义一个函数来执行加、减、乘、除操作,并将结果写入操作3文本框。最后,我们需要在窗口中绑定按钮和函数。下面是完整的代码:
```python
import tkinter as tk
# 定义计算函数
def calculate():
# 获取操作数1、操作数2和运算符
operand1 = entry1.get()
operand2 = entry2.get()
operator = var.get()
# 检查输入是否为纯数字
if not operand1.isdigit() or not operand2.isdigit():
result.set("输入错误")
return
# 将操作数转换为整数
operand1 = int(operand1)
operand2 = int(operand2)
# 执行加、减、乘、除操作
if operator == "+":
result.set(str(operand1 + operand2))
elif operator == "-":
result.set(str(operand1 - operand2))
elif operator == "*":
result.set(str(operand1 * operand2))
elif operator == "/":
if operand2 == 0:
result.set("除数不能为0")
return
result.set(str(operand1 / operand2))
# 创建窗口
root = tk.Tk()
root.title("简易计算器")
root.geometry("300x200")
# 添加文本框和标签
label1 = tk.Label(root, text="操作数1:")
label1.grid(row=0, column=0, padx=5, pady=5)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1, padx=5, pady=5)
label2 = tk.Label(root, text="操作数2:")
label2.grid(row=1, column=0, padx=5, pady=5)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1, padx=5, pady=5)
label3 = tk.Label(root, text="操作3:")
label3.grid(row=2, column=0, padx=5, pady=5)
result = tk.StringVar()
entry3 = tk.Entry(root, textvariable=result)
entry3.grid(row=2, column=1, padx=5, pady=5)
# 添加按钮
var = tk.StringVar()
var.set("+")
button1 = tk.Radiobutton(root, text="+", variable=var, value="+")
button1.grid(row=3, column=0, padx=5, pady=5)
button2 = tk.Radiobutton(root, text="-", variable=var, value="-")
button2.grid(row=3, column=1, padx=5, pady=5)
button3 = tk.Radiobutton(root, text="*", variable=var, value="*")
button3.grid(row=4, column=0, padx=5, pady=5)
button4 = tk.Radiobutton(root, text="/", variable=var, value="/")
button4.grid(row=4, column=1, padx=5, pady=5)
button5 = tk.Button(root, text="计算", command=calculate)
button5.grid(row=5, column=0, columnspan=2, padx=5, pady=5)
# 运行窗口
root.mainloop()
```
在这个程序中,我们使用了tkinter的各种布局和控件来创建窗口。通过设置按钮的value属性,我们可以轻松地获取用户选择的运算符。在calculate函数中,我们将输入的文本转换为整数,并执行相应的运算。如果发生错误,则将结果文本设置为错误消息。最后,我们将结果文本绑定到entry3的textvariable属性,以便在计算函数中更新结果。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![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)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)