1. 简易计算器,窗口界面自由发挥,编程实现两个整数的**加**、**减**、**乘**、**除**操作。要求如下: - 首先进行操作数文本框检验,检验输入数据是否为**纯数字**; - 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-23 13:10:33 浏览: 90
JAVA计算机小程序
以下是一个使用Python tkinter库实现的简易计算器窗口界面,可以实现两个整数的加减乘除操作:
```python
import tkinter as tk
def calculate():
try:
num1 = int(entry1.get())
num2 = int(entry2.get())
if operation.get() == "+":
result = num1 + num2
elif operation.get() == "-":
result = num1 - num2
elif operation.get() == "*":
result = num1 * num2
elif operation.get() == "/":
result = num1 / num2
entry3.delete(0, tk.END)
entry3.insert(0, result)
except ValueError:
entry3.delete(0, tk.END)
entry3.insert(0, "请输入纯数字")
root = tk.Tk()
root.title("简易计算器")
frame1 = tk.Frame(root)
frame1.pack()
label1 = tk.Label(frame1, text="操作数1:")
label1.pack(side=tk.LEFT)
entry1 = tk.Entry(frame1)
entry1.pack(side=tk.LEFT)
frame2 = tk.Frame(root)
frame2.pack()
label2 = tk.Label(frame2, text="操作数2:")
label2.pack(side=tk.LEFT)
entry2 = tk.Entry(frame2)
entry2.pack(side=tk.LEFT)
frame3 = tk.Frame(root)
frame3.pack()
label3 = tk.Label(frame3, text="操作符:")
label3.pack(side=tk.LEFT)
operation = tk.StringVar()
operation.set("+")
radiobutton1 = tk.Radiobutton(frame3, text="+", variable=operation, value="+")
radiobutton1.pack(side=tk.LEFT)
radiobutton2 = tk.Radiobutton(frame3, text="-", variable=operation, value="-")
radiobutton2.pack(side=tk.LEFT)
radiobutton3 = tk.Radiobutton(frame3, text="*", variable=operation, value="*")
radiobutton3.pack(side=tk.LEFT)
radiobutton4 = tk.Radiobutton(frame3, text="/", variable=operation, value="/")
radiobutton4.pack(side=tk.LEFT)
frame4 = tk.Frame(root)
frame4.pack()
button1 = tk.Button(frame4, text="计算", command=calculate)
button1.pack(side=tk.LEFT)
entry3 = tk.Entry(frame4)
entry3.pack(side=tk.LEFT)
root.mainloop()
```
运行该程序后,会弹出一个窗口界面,用户输入两个整数和操作符后,单击“计算”按钮即可进行计算,结果会显示在操作数3文本框中。如果用户输入的不是纯数字,则会在操作数3文本框中显示“请输入纯数字”。
阅读全文