设计一个500*500的图形化界面,界面中两个输入框中间一个+号,一个计算按钮,点击计算,完成两个数相加,在最右边结果框中显示结果,在下方的文本框中显示完整的计算过程。外加一个清除按钮,点击清除,将下方文本框内容清除
时间: 2024-03-09 15:50:33 浏览: 18
好的,这是一个简单的图形化界面设计问题。我可以提供一种基于Python Tkinter模块的解决方案,代码如下:
```python
import tkinter as tk
class CalculatorGUI:
def __init__(self, master):
self.master = master
master.title("简易计算器")
# 创建输入框和+号
self.num1_entry = tk.Entry(master, width=10)
self.num1_entry.grid(row=0, column=0)
self.plus_label = tk.Label(master, text="+")
self.plus_label.grid(row=0, column=1)
self.num2_entry = tk.Entry(master, width=10)
self.num2_entry.grid(row=0, column=2)
# 创建计算按钮和结果框
self.calculate_button = tk.Button(master, text="计算", command=self.calculate)
self.calculate_button.grid(row=0, column=3)
self.result_label = tk.Label(master, width=15)
self.result_label.grid(row=0, column=4)
# 创建清除按钮
self.clear_button = tk.Button(master, text="清除", command=self.clear)
self.clear_button.grid(row=1, column=3)
# 创建文本框
self.text_box = tk.Text(master, height=10)
self.text_box.grid(row=2, column=0, columnspan=5)
def calculate(self):
try:
num1 = float(self.num1_entry.get())
num2 = float(self.num2_entry.get())
result = num1 + num2
self.result_label.config(text=str(result))
self.text_box.insert(tk.END, f"{num1} + {num2} = {result}\n")
except ValueError:
self.result_label.config(text="Error")
self.text_box.insert(tk.END, "输入错误\n")
def clear(self):
self.text_box.delete("1.0", tk.END)
root = tk.Tk()
my_gui = CalculatorGUI(root)
root.mainloop()
```
这个界面包括两个输入框、一个加号、一个计算按钮、一个结果框、一个清除按钮和一个文本框。当用户点击计算按钮时,程序会将两个输入框中的数相加,并将结果显示在结果框中。同时,程序会将完整的计算过程显示在文本框中。当用户点击清除按钮时,程序会清空文本框中的内容。
相关推荐
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.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)
![](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)