创建一个窗口,用户在entry组件上输入圆的半径,单击“计算”按钮,在另两个entry组件上分别输出圆的面积和周长
时间: 2024-03-05 07:49:05 浏览: 51
_ATL_OBJMAP_ENTRY定义-COM组件开发PPT
好的,这是一个简单的Python Tkinter GUI程序来实现你的需求:
```python
import tkinter as tk
def calculate():
try:
radius = float(radius_entry.get())
area = round(3.14 * radius ** 2, 2)
perimeter = round(2 * 3.14 * radius, 2)
area_entry.delete(0, tk.END)
perimeter_entry.delete(0, tk.END)
area_entry.insert(0, str(area))
perimeter_entry.insert(0, str(perimeter))
except ValueError:
area_entry.delete(0, tk.END)
perimeter_entry.delete(0, tk.END)
area_entry.insert(0, "Invalid Input")
perimeter_entry.insert(0, "Invalid Input")
# 创建主窗口
window = tk.Tk()
window.title("Circle Calculator")
# 创建组件
radius_label = tk.Label(window, text="半径:")
radius_entry = tk.Entry(window)
area_label = tk.Label(window, text="面积:")
area_entry = tk.Entry(window)
perimeter_label = tk.Label(window, text="周长:")
perimeter_entry = tk.Entry(window)
calculate_button = tk.Button(window, text="计算", command=calculate)
# 组件布局
radius_label.grid(row=0, column=0)
radius_entry.grid(row=0, column=1)
area_label.grid(row=1, column=0)
area_entry.grid(row=1, column=1)
perimeter_label.grid(row=2, column=0)
perimeter_entry.grid(row=2, column=1)
calculate_button.grid(row=3, column=1)
# 运行主循环
window.mainloop()
```
当用户在半径输入框中输入值并单击“计算”按钮时,程序将计算圆的面积和周长,并在相应的输入框中显示结果。如果输入无效,则在输入框中显示“Invalid Input”。
阅读全文