1. 简易计算器,窗口界面自由发挥,编程实现两个整数的**加**、**减**、**乘**、**除**操作。要求如下: - 首先进行操作数文本框检验,检验输入数据是否为**纯数字**; - 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框。
时间: 2023-07-23 19:14:05 浏览: 58
JAVA计算机小程序
好的,这是一个简单的计算器程序,可以使用Python的tkinter库来创建窗口界面,并编写相应的代码实现加、减、乘、除操作。下面是一个示例代码:
```python
import tkinter as tk
class Calculator:
def __init__(self, master):
self.master = master
master.title("Calculator")
self.label1 = tk.Label(master, text="Operand 1:")
self.label1.grid(row=0, column=0)
self.entry1 = tk.Entry(master)
self.entry1.grid(row=0, column=1)
self.label2 = tk.Label(master, text="Operand 2:")
self.label2.grid(row=1, column=0)
self.entry2 = tk.Entry(master)
self.entry2.grid(row=1, column=1)
self.label3 = tk.Label(master, text="Result:")
self.label3.grid(row=2, column=0)
self.entry3 = tk.Entry(master, state="readonly")
self.entry3.grid(row=2, column=1)
self.add_button = tk.Button(master, text="+", command=self.add)
self.add_button.grid(row=3, column=0)
self.subtract_button = tk.Button(master, text="-", command=self.subtract)
self.subtract_button.grid(row=3, column=1)
self.multiply_button = tk.Button(master, text="*", command=self.multiply)
self.multiply_button.grid(row=4, column=0)
self.divide_button = tk.Button(master, text="/", command=self.divide)
self.divide_button.grid(row=4, column=1)
def add(self):
operand1 = self.entry1.get()
operand2 = self.entry2.get()
if operand1.isdigit() and operand2.isdigit():
result = int(operand1) + int(operand2)
self.entry3.configure(state="normal")
self.entry3.delete(0, tk.END)
self.entry3.insert(0, str(result))
self.entry3.configure(state="readonly")
def subtract(self):
operand1 = self.entry1.get()
operand2 = self.entry2.get()
if operand1.isdigit() and operand2.isdigit():
result = int(operand1) - int(operand2)
self.entry3.configure(state="normal")
self.entry3.delete(0, tk.END)
self.entry3.insert(0, str(result))
self.entry3.configure(state="readonly")
def multiply(self):
operand1 = self.entry1.get()
operand2 = self.entry2.get()
if operand1.isdigit() and operand2.isdigit():
result = int(operand1) * int(operand2)
self.entry3.configure(state="normal")
self.entry3.delete(0, tk.END)
self.entry3.insert(0, str(result))
self.entry3.configure(state="readonly")
def divide(self):
operand1 = self.entry1.get()
operand2 = self.entry2.get()
if operand1.isdigit() and operand2.isdigit() and int(operand2) != 0:
result = int(operand1) / int(operand2)
self.entry3.configure(state="normal")
self.entry3.delete(0, tk.END)
self.entry3.insert(0, str(result))
self.entry3.configure(state="readonly")
root = tk.Tk()
calculator = Calculator(root)
root.mainloop()
```
运行程序后,会出现一个窗口界面,其中包括两个操作数文本框和一个结果文本框,以及四个按钮用于进行加、减、乘、除操作。用户在输入操作数时,会对其进行检验,只有在输入的数据为纯数字时才能进行计算。用户单击按钮后,程序会根据用户选择的操作对操作数1和操作数2进行相应的计算,并将结果显示在结果文本框中。
阅读全文