TProgressBarDemo 项目分析¶
项目信息¶
- 项目名称: TProgressBarDemo
- 下载链接: TProgressBarDemo.rar
- 分析时间: 2026-03-05
文件结构¶
TProgressBarDemo/
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm
主程序文件: Project1.py¶
from glcl import *
from Unit1 import *
def main():
Application.Initialize()
Application.Title = 'Project1'
Application.MainFormOnTaskbar = True
Application.Icon.LoadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico"))
MainForm = Form1(Application)
MainForm.Show()
FreeConsole()
Application.Run()
MainForm.Free()
if __name__ == '__main__':
main()
单元文件: Unit1.py¶
import os
from glcl import *
import time
class Form1(Form):
def __init__(self, owner):
self.Label1 = Label(self)
self.Button1 = Button(self)
self.ProgressBar1 = ProgressBar(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
self.Button1.OnClick = self.Button1Click
# 创建 TProgressBar 控件
self.progress_bar = ProgressBar(self)
self.progress_bar.SetBounds(20, 50, 360, 30) # 设置 ProgressBar 的位置和大小
self.progress_bar.Min = 0 # 设置最小值
self.progress_bar.Max = 100 # 设置最大值
self.progress_bar.Position = 0 # 设置初始位置
self.progress_bar.Parent = self # 将 ProgressBar 设置为窗体的子控件
# 创建一个按钮用于开始进度
self.start_button = Button(self)
self.start_button.SetBounds(20, 100, 100, 30) # 设置按钮的位置和大小
self.start_button.Caption = "开始" # 设置按钮文本
self.start_button.OnClick = self.start_progress # 绑定点击事件
self.start_button.Parent = self # 将按钮设置为窗体的子控件
# 创建一个标签显示当前进度
self.label = Label(self)
self.label.SetBounds(150, 100, 230, 30) # 设置标签的位置和大小
self.label.Caption = "进度: 0%" # 初始化标签文本
self.label.Parent = self # 将标签设置为窗体的子控件
# 开始进度的方法
def start_progress(self, sender):
# 在进度条上进行逐步增加进度
for i in range(self.progress_bar.Min, self.progress_bar.Max + 1):
self.progress_bar.Position = i # 设置当前进度
self.label.Caption = f"进度: {i}%" # 更新标签显示进度
Application.ProcessMessages() # 处理消息以更新界面
time.sleep(0.05) # 模拟延迟,增加进度的时间间隔
def Button1Click(self, Sender):
# 在进度条上进行逐步增加进度
for i in range(self.ProgressBar1.Min, self.ProgressBar1.Max + 1):
self.ProgressBar1.Position = i # 设置当前进度
self.Label1.Caption = f"进度: {i}%" # 更新标签显示进度
Application.ProcessMessages() # 处理消息以更新界面
time.sleep(0.05) # 模拟延迟,增加进度的时间间隔
设计文件: Unit1.sct¶
def Button1Click(Sender):
设计文件: Unit1.sfm¶
object Form1: TForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 205
ClientWidth = 382
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = 'Segoe UI'
Font.Style = []
TextHeight = 20
object Label1: TLabel
Left = 8
Top = 168
Width = 44
Height = 20
Caption = 'Label1'
end
object ProgressBar1: TProgressBar
Left = 8
Top = 136
Width = 262
Height = 21
TabOrder = 0
end
object Button1: TButton
Left = 288
Top = 136
Width = 78
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
end
其他文件¶
- Extractor_Icon.ico
- Project1.xml
详细分析¶
功能概述¶
GUI组件演示
使用的组件¶
- Form1 (TForm)
- Label1 (TLabel)
- ProgressBar1 (TProgressBar)
- Button1 (TButton)
技术特点¶
- 包含4个GUI组件
- 定义2个事件处理
窗体属性¶
- caption: Button1
- height: 205
- width: 382
代码分析¶
导入的模块: - import os - from glcl import * - import time
定义的类: - Form1
定义的方法: - init - start_progress - Button1Click
事件绑定: - self.Button1.OnClick = self.Button1Click - self.start_button.OnClick = self.start_progress # 绑定点击事件