Skip to content

TDirectoryListBoxDemo 项目分析

项目信息

文件结构

TDirectoryListBoxDemo/
├── 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.DirectoryListBox1 = DirectoryListBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click
        self.DirectoryListBox1.OnChange = self.DirectoryListBox1Change

        # 创建一个 TDirectoryListBox 控件
        self.directory_list_box = DirectoryListBox(self)
        self.directory_list_box.Parent = self   # 设置父控件为窗体
        self.directory_list_box.Left = 10       # 设置左边距离
        self.directory_list_box.Top = 50        # 设置顶部距离
        self.directory_list_box.Height = 300    # 设置高度
        self.directory_list_box.Directory = 'C:\\'  # 设置初始目录为C盘根目录

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

    # 按钮点击事件处理函数
    def show_selected_directory(self, Sender):
        selected_directory = self.directory_list_box.Directory  # 获取所选目录
        if selected_directory:
            ShowMessage(f'所选目录: {selected_directory}')  # 显示所选目录
        else:
            ShowMessage('没有选择目录')  # 如果没有选择目录,则显示提示

    def DirectoryListBox1Change(self, Sender):
        self.FileListBox1.Directory = self.DirectoryListBox1.Directory

    def Button1Click(self, Sender):
        self.FileListBox1.Mask = '*.py'

设计文件: Unit1.sct

def DirectoryListBox1Change(Sender): 
def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 438
  ClientWidth = 566
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object DirectoryListBox1: TDirectoryListBox
    Left = 280
    Top = 40
    Width = 257
    Height = 185
    ItemHeight = 20
    TabOrder = 0
    OnChange = DirectoryListBox1Change
  end
  object FileListBox1: TFileListBox
    Left = 280
    Top = 232
    Width = 257
    Height = 129
    ItemHeight = 20
    TabOrder = 1
  end
  object Button1: TButton
    Left = 280
    Top = 368
    Width = 257
    Height = 25
    Caption = #35774#32622#36807#28388#21482#26174#31034'py'#25991#20214
    TabOrder = 2
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

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

技术特点

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

窗体属性

  • caption: #35774#32622#36807#28388#21482#26174#31034'py'#25991#20214
  • height: 438
  • width: 566

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_directory - DirectoryListBox1Change - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.DirectoryListBox1.OnChange = self.DirectoryListBox1Change - self.show_directory_button.OnClick = self.show_selected_directory # 设置按钮点击事件处理