Skip to content

TDriveComboBoxDemo 项目分析

项目信息

文件结构

TDriveComboBoxDemo/
├── 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.FileListBox1 = FileListBox(self)
        self.DirectoryListBox1 = DirectoryListBox(self)
        self.DriveComboBox1 = DriveComboBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        # DriveComboBox可以设置DirList、DirectoryListBox设置FileList以实现三个控件联动

        # 创建一个 TDriveComboBox 控件
        self.drive_combo_box = DriveComboBox(self)
        self.drive_combo_box.Parent = self  # 设置父控件为窗体
        self.drive_combo_box.Align = alTop  # 将组合框置顶对齐

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

    # 按钮点击事件处理函数
    def show_selected_drive(self, Sender):
        selected_drive = self.drive_combo_box.Drive  # 获取所选驱动器
        if selected_drive:
            ShowMessage(f'所选驱动器: {selected_drive}')  # 显示所选驱动器
        else:
            ShowMessage('没有选择驱动器')  # 如果没有选择驱动器,则显示提示

设计文件: Unit1.sct


设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 340
  ClientWidth = 722
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object DriveComboBox1: TDriveComboBox
    Left = 8
    Top = 96
    Width = 361
    Height = 26
    DirList = DirectoryListBox1
    TabOrder = 0
  end
  object DirectoryListBox1: TDirectoryListBox
    Left = 8
    Top = 128
    Width = 361
    Height = 201
    FileList = FileListBox1
    ItemHeight = 20
    TabOrder = 1
  end
  object FileListBox1: TFileListBox
    Left = 376
    Top = 128
    Width = 337
    Height = 201
    ItemHeight = 20
    TabOrder = 2
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • DriveComboBox1 (TDriveComboBox)
  • DirectoryListBox1 (TDirectoryListBox)
  • FileListBox1 (TFileListBox)

技术特点

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

窗体属性

  • caption: Form1
  • height: 340
  • width: 722

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_drive

事件绑定: - self.show_drive_button.OnClick = self.show_selected_drive # 设置按钮点击事件处理