![ps_圆的面积demo]()
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
import os
from glcl import *
class Form1(Form):
def __init__(self, owner):
self.Button3 = Button(self)
self.Button2 = Button(self)
self.Button1 = Button(self)
self.SegoeGUI = Label(self)
self.Edit1 = Edit(self)
self.Label2 = Label(self)
self.Label1 = Label(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
self.Button3.OnClick = self.Button3Click
self.Button2.OnClick = self.Button2Click
self.Button1.OnClick = self.Button1Click
def Button1Click(self, Sender):
r=float(self.edit1.text)
s=3.14*r**2
self.label3.caption="面积:"+str(s)
def Button2Click(self, Sender):
self.Edit1.Clear( )
self.Edit1.SetFocus( )
self.label3.Caption = "面积:"
def Button3Click(self, Sender):
self.Close()
下面是用python TK库写的同样程序,对比一下代码量
import tkinter as tk
import math
def calculate_area():
try:
radius = float(radius_entry.get())
area = math.pi * radius ** 2
area_label.config(text=f"{area}")
except ValueError:
area_label.config(text="请输入有效的半径值")
def clear_fields():
radius_entry.delete(0, tk.END)
area_label.config(text="")
root = tk.Tk()
root.title("圆面积计算程序")
root.geometry("300x200")
root.configure(bg="purple")
# 设置标题
title_label = tk.Label(root, text="计算圆的面积", font=("Arial", 16), bg="purple", fg="white")
title_label.pack(pady=10)
# 半径输入部分
radius_frame = tk.Frame(root, bg="purple")
radius_frame.pack()
radius_label = tk.Label(radius_frame, text="半径:", bg="purple", fg="white")
radius_label.pack(side=tk.LEFT)
radius_entry = tk.Entry(radius_frame, width=10)
radius_entry.pack(side=tk.LEFT)
# 面积显示部分
area_frame = tk.Frame(root, bg="purple")
area_frame.pack()
area_label_text = tk.Label(area_frame, text="面积:", bg="purple", fg="white")
area_label_text.pack(side=tk.LEFT)
area_label = tk.Label(area_frame, text="", width=15)
area_label.pack(side=tk.LEFT)
# 按钮部分
button_frame = tk.Frame(root, bg="purple")
button_frame.pack(pady=10)
calculate_button = tk.Button(button_frame, text="计算", command=calculate_area)
calculate_button.pack(side=tk.LEFT, padx=5)
clear_button = tk.Button(button_frame, text="清空", command=clear_fields)
clear_button.pack(side=tk.LEFT, padx=5)
exit_button = tk.Button(button_frame, text="退出", command=root.quit)
exit_button.pack(side=tk.LEFT, padx=5)
root.mainloop()