Skip to content

TFilterComboBoxDemo 项目分析

项目信息

文件结构

TFilterComboBoxDemo/
├── 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.DriveComboBox1 = DriveComboBox(self)
        self.DirectoryListBox1 = DirectoryListBox(self)
        self.FileListBox1 = FileListBox(self)
        self.FilterComboBox1 = FilterComboBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        # 可以设置FilterComboBox的FileList属性为FileListBox
        # Filter属性注意书写格式,每一组中间用'|'符号隔开,例如 文本文件 (*.txt)|*.txt

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

        # 添加一些示例过滤器到 TFilterComboBox
        self.filter_combo_box.Items.Add('所有文件 (*.*)')
        self.filter_combo_box.Items.Add('文本文件 (*.txt)')
        self.filter_combo_box.Items.Add('图像文件 (*.png; *.jpg; *.jpeg)')
        self.filter_combo_box.Items.Add('可执行文件 (*.exe)')

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

    # 按钮点击事件处理函数
    def show_selected_filter(self, Sender):
        selected_filter = self.filter_combo_box.Text  # 获取所选过滤器
        if selected_filter:
            ShowMessage(f'所选过滤器: {selected_filter}')  # 显示所选过滤器
        else:
            ShowMessage('没有选择过滤器')  # 如果没有选择过滤器,则显示提示

设计文件: Unit1.sct


设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 515
  ClientWidth = 548
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object FilterComboBox1: TFilterComboBox
    Left = 8
    Top = 480
    Width = 529
    Height = 28
    FileList = FileListBox1
    Filter = 'All files (*.*)|*.*|Py file (*.py)|*.py'
    TabOrder = 0
  end
  object FileListBox1: TFileListBox
    Left = 8
    Top = 328
    Width = 529
    Height = 145
    ItemHeight = 20
    TabOrder = 1
  end
  object DirectoryListBox1: TDirectoryListBox
    Left = 8
    Top = 144
    Width = 529
    Height = 177
    FileList = FileListBox1
    ItemHeight = 20
    TabOrder = 2
  end
  object DriveComboBox1: TDriveComboBox
    Left = 8
    Top = 112
    Width = 529
    Height = 26
    DirList = DirectoryListBox1
    TabOrder = 3
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

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

技术特点

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

窗体属性

  • caption: Form1
  • height: 515
  • width: 548

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_filter

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