Skip to content

TCheckListBoxDemo 项目分析

项目信息

文件结构

TCheckListBoxDemo/
├── 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.CheckListBox1 = CheckListBox(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click
        self.CheckListBox1.OnClickCheck = self.CheckListBox1ClickCheck

        # 创建 TCheckListBox 组件
        self.check_list_box = CheckListBox(self)
        self.check_list_box.Parent = self  # 设置父控件为当前窗体
        self.check_list_box.Left = 30  # 设置左边距30
        self.check_list_box.Top = 30  # 设置顶边距30

        # 向 TCheckListBox 添加项目
        self.check_list_box.Items.Add("选项 1")
        self.check_list_box.Items.Add("选项 2")
        self.check_list_box.Items.Add("选项 3")
        self.check_list_box.Items.Add("选项 4")

        # 创建一个按钮用于显示选中项
        self.button = Button(self)
        self.button.Parent = self  # 设置父控件为当前窗体
        self.button.Width = 150  # 设置宽度为150
        self.button.Left = 30  # 设置左边距30
        self.button.Top = 200  # 设置顶边距200
        self.button.Caption = "显示选中的项"
        self.button.OnClick = self.show_selected_items  # 绑定按钮点击事件

    def show_selected_items(self, Sender):
        # 获取选中的项
        selected_items = [self.check_list_box.Items[i] for i in range(self.check_list_box.Count) if self.check_list_box.Checked[i]]

        # 使用消息框显示选中的项
        if selected_items:
            ShowMessage("选中的项:\n" + "\n".join(selected_items))
        else:
            ShowMessage("没有选中的项")

    def CheckListBox1ClickCheck(self, Sender):
        ShowMessage(self.CheckListBox1.ItemIndex)

    def Button1Click(self, Sender):
        # 获取选中的项
        selected_items = [self.CheckListBox1.Items[i] for i in range(self.CheckListBox1.Count) if self.CheckListBox1.Checked[i]]

        # 使用消息框显示选中的项
        if selected_items:
            ShowMessage("选中的项:\n" + "\n".join(selected_items))
        else:
            ShowMessage("没有选中的项")

设计文件: Unit1.sct

def CheckListBox1ClickCheck(Sender): 
def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 432
  ClientWidth = 270
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object CheckListBox1: TCheckListBox
    Left = 16
    Top = 248
    Width = 169
    Height = 145
    ItemHeight = 20
    Items.Strings = (
      #39321#34121
      #33529#26524
      #33889#33796
      #21704#23494#29916
      #27185#26691
      #34013#33683)
    TabOrder = 0
    OnClickCheck = CheckListBox1ClickCheck
  end
  object Button1: TButton
    Left = 78
    Top = 400
    Width = 107
    Height = 25
    Caption = #33719#21462#36873#20013#30340#39033
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • CheckListBox1 (TCheckListBox)
  • Button1 (TButton)

技术特点

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

窗体属性

  • caption: #33719#21462#36873#20013#30340#39033
  • height: 432
  • width: 270

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_items - CheckListBox1ClickCheck - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.button.OnClick = self.show_selected_items # 绑定按钮点击事件