简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: 首先进行操作数文本框检验,检验输入数据是否为纯数字; 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-10 17:10:03 浏览: 38
好的,这是一个比较简单的任务,我可以为您提供一个Python的代码示例,用Tkinter构建一个简单的GUI界面来实现这个计算器。以下是代码:
```python
import tkinter as tk
def calculate():
try:
num1 = int(entry1.get())
num2 = int(entry2.get())
if operator.get() == "+":
result = num1 + num2
elif operator.get() == "-":
result = num1 - num2
elif operator.get() == "*":
result = num1 * num2
elif operator.get() == "/":
result = num1 / num2
entry3.delete(0, tk.END)
entry3.insert(0, result)
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "Error: invalid input")
root = tk.Tk()
root.title("Simple Calculator")
# label and entry for operand 1
label1 = tk.Label(root, text="Operand 1:")
label1.grid(row=0, column=0)
entry1 = tk.Entry(root)
entry1.grid(row=0, column=1)
# label and entry for operand 2
label2 = tk.Label(root, text="Operand 2:")
label2.grid(row=1, column=0)
entry2 = tk.Entry(root)
entry2.grid(row=1, column=1)
# label and entry for result
label3 = tk.Label(root, text="Result:")
label3.grid(row=2, column=0)
entry3 = tk.Entry(root)
entry3.grid(row=2, column=1)
# radio buttons for operator
operator = tk.StringVar()
operator.set("+")
add_button = tk.Radiobutton(root, text="+", variable=operator, value="+")
add_button.grid(row=3, column=0)
sub_button = tk.Radiobutton(root, text="-", variable=operator, value="-")
sub_button.grid(row=3, column=1)
mul_button = tk.Radiobutton(root, text="*", variable=operator, value="*")
mul_button.grid(row=3, column=2)
div_button = tk.Radiobutton(root, text="/", variable=operator, value="/")
div_button.grid(row=3, column=3)
# calculate button
calc_button = tk.Button(root, text="Calculate", command=calculate)
calc_button.grid(row=4, column=1)
root.mainloop()
```
在这个示例代码中,我们使用了Tkinter库来构建一个简单的GUI界面。我们在界面上放置了三个输入框,分别用于输入操作数1、操作数2和结果。我们还使用了四个单选按钮,用于选择加、减、乘、除运算符。最后,我们在界面上放置了一个“计算”按钮,用于执行计算操作。
在calculate函数中,我们首先从输入框中获取操作数1和操作数2的值,并根据所选的运算符执行相应的计算。如果输入的值不是纯数字,则会在结果框中显示错误信息。
希望这个简单的示例代码能够帮助您完成这个任务,如果您对代码有任何疑问或需要进一步的帮助,请随时问我。
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![cpp](https://img-home.csdnimg.cn/images/20210720083646.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![EXE](https://img-home.csdnimg.cn/images/20210720083646.png)
![java](https://img-home.csdnimg.cn/images/20210720083646.png)
![pptx](https://img-home.csdnimg.cn/images/20210720083543.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)