简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: - 首先进行操作数文本框检验,检验输入数据是否为纯数字; - 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-10 07:23:16 浏览: 26
好的,这是一个简单的计算器程序,使用Python的Tkinter库来创建窗口界面,以下是完整的代码实现。
```python
import tkinter as tk
def is_number(num):
try:
float(num)
return True
except ValueError:
return False
def calculate():
num1 = entry1.get()
num2 = entry2.get()
if not is_number(num1) or not is_number(num2):
result.set("请输入数字!")
return
num1 = float(num1)
num2 = float(num2)
if operator.get() == "+":
result.set(num1 + num2)
elif operator.get() == "-":
result.set(num1 - num2)
elif operator.get() == "*":
result.set(num1 * num2)
elif operator.get() == "/":
if num2 == 0:
result.set("除数不能为0!")
return
result.set(num1 / num2)
root = tk.Tk()
root.title("简易计算器")
root.geometry("250x150")
operator = tk.StringVar()
operator.set("+")
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)
plus_button = tk.Radiobutton(root, text="+", variable=operator, value="+")
plus_button.grid(row=2, column=1)
minus_button = tk.Radiobutton(root, text="-", variable=operator, value="-")
minus_button.grid(row=2, column=2)
multiply_button = tk.Radiobutton(root, text="*", variable=operator, value="*")
multiply_button.grid(row=2, column=3)
divide_button = tk.Radiobutton(root, text="/", variable=operator, value="/")
divide_button.grid(row=2, column=4)
calculate_button = tk.Button(root, text="计算", command=calculate)
calculate_button.grid(row=3, column=2)
label4 = tk.Label(root, text="操作结果")
label4.grid(row=4, column=0)
result = tk.StringVar()
result.set("0")
entry3 = tk.Entry(root, textvariable=result, state="readonly")
entry3.grid(row=4, column=1)
root.mainloop()
```
运行程序后,会弹出一个250x150像素大小的窗口,包含两个操作数的文本框和一个操作符的单选按钮组,以及一个计算按钮和一个操作结果的文本框。用户可以通过单击单选按钮来选择要进行的操作符,然后单击计算按钮来计算结果。在计算结果之前,程序会检查操作数是否为数字,如果不是则会显示一个错误消息。如果用户选择了除法操作并且除数为0,则同样会显示一个错误消息。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.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)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)