Skip to content

TFileListBoxDemo 项目分析

项目信息

文件结构

TFileListBoxDemo/
├── 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.Button1 = Button(self)
        self.FileListBox1 = FileListBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建一个 TFileListBox 控件
        self.file_list_box = FileListBox(self)
        self.file_list_box.Parent = self    # 设置父控件为窗体
        self.file_list_box.Left = 10        # 左边距离
        self.file_list_box.Top = 50         # 上边距离
        self.file_list_box.Width = 200      # 宽度
        self.file_list_box.Height = 300     # 高度
        self.file_list_box.Directory = '.'  # 设置初始目录为当前目录

        # 创建一个按钮,用于显示所选文件名
        self.show_file_button = Button(self)
        self.show_file_button.Parent = self # 设置父控件为窗体
        self.show_file_button.Caption = '显示所选文件'  # 设置按钮文本
        self.show_file_button.Top = 10  # 设置按钮距离顶部的距离
        self.show_file_button.Left = 10  # 设置按钮距离左侧的距离
        self.show_file_button.Width = 200   # 宽度
        self.show_file_button.OnClick = self.show_selected_file  # 设置按钮点击事件处理

    # 按钮点击事件处理函数
    def show_selected_file(self, Sender):
        selected_file = self.file_list_box.FileName  # 获取所选文件名
        if selected_file:
            ShowMessage(f'所选文件: {selected_file}')  # 显示所选文件名
        else:
            ShowMessage('没有选择文件')  # 如果没有选择文件,则显示提示

    def Button1Click(self, Sender):
        # 设置目录
        self.FileListBox1.Directory = Application.SelectDirectory('选择目录', 'C:\\')
        # 设置窗体获得焦点,若不调用此函数,在选择目录对话框关闭后,窗体会无焦点
        self.SetFocus()

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 355
  ClientWidth = 489
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object FileListBox1: TFileListBox
    Left = 240
    Top = 24
    Width = 233
    Height = 257
    ItemHeight = 20
    TabOrder = 0
  end
  object Button1: TButton
    Left = 398
    Top = 288
    Width = 75
    Height = 25
    Caption = #36873#25321#30446#24405
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • FileListBox1 (TFileListBox)
  • Button1 (TButton)

技术特点

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

窗体属性

  • caption: #36873#25321#30446#24405
  • height: 355
  • width: 489

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_file - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.show_file_button.OnClick = self.show_selected_file # 设置按钮点击事件处理