简易计算器,窗口界面自由发挥,编程实现两个整数的加、减、乘、除操作。要求如下: (1). 首先进行操作数文本框检验,检验输入数据是否为纯数字; (2). 通过分别单击加、减、乘、除按钮,对操作数1与操作数2进行相应加、减、乘、除操作,并且将其结果写入操作3文本框
时间: 2023-07-10 08:14:20 浏览: 95
好的,这是一个简单的计算器界面和实现代码:
```python
import tkinter as tk
class Calculator:
def __init__(self, root):
self.root = root
self.root.title("简易计算器")
self.root.geometry("300x200")
# 操作数1文本框
self.num1_label = tk.Label(self.root, text="操作数1")
self.num1_label.grid(row=0, column=0, padx=5, pady=5, sticky=tk.W)
self.num1_entry = tk.Entry(self.root)
self.num1_entry.grid(row=0, column=1, padx=5, pady=5)
# 操作数2文本框
self.num2_label = tk.Label(self.root, text="操作数2")
self.num2_label.grid(row=1, column=0, padx=5, pady=5, sticky=tk.W)
self.num2_entry = tk.Entry(self.root)
self.num2_entry.grid(row=1, column=1, padx=5, pady=5)
# 操作3文本框
self.num3_label = tk.Label(self.root, text="结果")
self.num3_label.grid(row=2, column=0, padx=5, pady=5, sticky=tk.W)
self.num3_entry = tk.Entry(self.root, state="readonly")
self.num3_entry.grid(row=2, column=1, padx=5, pady=5)
# 加、减、乘、除按钮
self.add_button = tk.Button(self.root, text="+", command=self.add)
self.add_button.grid(row=3, column=0, padx=5, pady=5)
self.sub_button = tk.Button(self.root, text="-", command=self.sub)
self.sub_button.grid(row=3, column=1, padx=5, pady=5)
self.mul_button = tk.Button(self.root, text="*", command=self.mul)
self.mul_button.grid(row=4, column=0, padx=5, pady=5)
self.div_button = tk.Button(self.root, text="/", command=self.div)
self.div_button.grid(row=4, column=1, padx=5, pady=5)
# 加法
def add(self):
try:
num1 = float(self.num1_entry.get())
num2 = float(self.num2_entry.get())
result = num1 + num2
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, result)
self.num3_entry.config(state="readonly")
except ValueError:
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, "请输入数字")
self.num3_entry.config(state="readonly")
# 减法
def sub(self):
try:
num1 = float(self.num1_entry.get())
num2 = float(self.num2_entry.get())
result = num1 - num2
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, result)
self.num3_entry.config(state="readonly")
except ValueError:
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, "请输入数字")
self.num3_entry.config(state="readonly")
# 乘法
def mul(self):
try:
num1 = float(self.num1_entry.get())
num2 = float(self.num2_entry.get())
result = num1 * num2
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, result)
self.num3_entry.config(state="readonly")
except ValueError:
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, "请输入数字")
self.num3_entry.config(state="readonly")
# 除法
def div(self):
try:
num1 = float(self.num1_entry.get())
num2 = float(self.num2_entry.get())
if num2 == 0:
raise ZeroDivisionError
result = num1 / num2
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, result)
self.num3_entry.config(state="readonly")
except ValueError:
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, "请输入数字")
self.num3_entry.config(state="readonly")
except ZeroDivisionError:
self.num3_entry.config(state="normal")
self.num3_entry.delete(0, tk.END)
self.num3_entry.insert(0, "除数不能为0")
self.num3_entry.config(state="readonly")
if __name__ == '__main__':
root = tk.Tk()
app = Calculator(root)
root.mainloop()
```
这个计算器界面有三个文本框和四个按钮,通过检查文本框中输入的内容是不是数字,实现了加、减、乘、除操作,将结果写入操作3文本框。
阅读全文