Skip to content

TScrollBarDemo 项目分析

项目信息

文件结构

TScrollBarDemo/
├── 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 *

class Form1(Form):

    def __init__(self, owner):
        self.Label1 = Label(self)
        self.ScrollBar1 = ScrollBar(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.ScrollBar1.OnChange = self.ScrollBar1Change

        # 创建滚动条
        self.scroll_bar = ScrollBar(self)
        self.scroll_bar.Parent = self  # 设置滚动条的父控件为当前窗口
        self.scroll_bar.Min = 0  # 滚动条最小值
        self.scroll_bar.Max = 100  # 滚动条最大值
        self.scroll_bar.Position = 50  # 初始位置
        self.scroll_bar.Left = 40  # 左边距
        self.scroll_bar.Top = 80  # 上边距
        self.scroll_bar.Width = 400  # 宽度
        self.scroll_bar.Position = 0  # 滑块位置

        # 绑定滚动条的OnChange事件
        self.scroll_bar.OnChange = self.on_scroll_change

    def on_scroll_change(self, sender):
        # 当滚动条值变化时更新标签内容
        self.Label1.Caption = f'Scroll Value: {self.scroll_bar.Position}'

    def ScrollBar1Change(self, Sender):
        self.Label1.Caption = self.ScrollBar1.Position

设计文件: Unit1.sct

def ScrollBar1Change(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 380
  ClientWidth = 433
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Label1: TLabel
    Left = 32
    Top = 24
    Width = 44
    Height = 20
    Caption = 'Label1'
  end
  object ScrollBar1: TScrollBar
    Left = 400
    Top = 24
    Width = 17
    Height = 329
    Kind = sbVertical
    PageSize = 0
    TabOrder = 0
    OnChange = ScrollBar1Change
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • Label1 (TLabel)
  • ScrollBar1 (TScrollBar)

技术特点

  • 包含3个GUI组件
  • 定义2个事件处理

窗体属性

  • caption: Label1
  • height: 380
  • width: 433

代码分析

导入的模块: - import os - from glcl import *

定义的类: - Form1

定义的方法: - init - on_scroll_change - ScrollBar1Change

事件绑定: - self.ScrollBar1.OnChange = self.ScrollBar1Change - self.scroll_bar.OnChange = self.on_scroll_change