Skip to content

TStringGridDemo 项目分析

项目信息

文件结构

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

        # 创建 TStringGrid 控件
        self.string_grid = StringGrid(self)
        self.string_grid.Parent = self  # 设置父控件
        self.string_grid.Left = 20  # 设置控件的左侧位置
        self.string_grid.Top = 20  # 设置控件的顶部位置
        self.string_grid.Width = 360  # 设置控件宽度
        self.string_grid.Height = 200  # 设置控件高度

        # 设置 TStringGrid 的行列数
        self.string_grid.RowCount = 5  # 行数
        self.string_grid.ColCount = 3  # 列数

        # 设置列标题
        self.string_grid.SetCell(0, 0, "姓名")  # 第一列标题
        self.string_grid.SetCell(1, 0, "年龄")  # 第二列标题
        self.string_grid.SetCell(2, 0, "城市")  # 第三列标题

        # 添加一些示例数据
        self.string_grid.SetCell(0, 1, "Alice")  # 第一行第一列
        self.string_grid.SetCell(1, 1, "30")     # 第一行第二列
        self.string_grid.SetCell(2, 1, "北京")   # 第一行第三列

        self.string_grid.SetCell(0, 2, "Bob")    # 第二行第一列
        self.string_grid.SetCell(1, 2, "25")     # 第二行第二列
        self.string_grid.SetCell(2, 2, "上海")   # 第二行第三列

        # 创建按钮用于显示所选单元格的值
        self.show_button = Button(self)
        self.show_button.Parent = self  # 设置父控件
        self.show_button.Caption = "显示选中单元格"  # 设置按钮文本
        self.show_button.Left = 20  # 设置按钮的左侧位置
        self.show_button.Top = 230  # 设置按钮的顶部位置
        self.show_button.Width = 150  # 设置按钮的宽度
        self.show_button.OnClick = self.on_show_button_click  # 设置点击事件处理函数

    def on_show_button_click(self, Sender):
        # 获取选中的单元格的值
        row = self.string_grid.Row  # 获取当前选中的行
        col = self.string_grid.Col  # 获取当前选中的列
        cell_value = self.string_grid.GetCell(col, row)  # 获取单元格的值

        # 弹出消息框显示选中单元格的值
        ShowMessage(f"选中单元格的值: {cell_value}")  # 弹出消息框

    def Button1Click(self, Sender):
        # 设置 TStringGrid 的行列数
        self.StringGrid1.RowCount = 5  # 行数
        self.StringGrid1.ColCount = 3  # 列数

        # 设置列标题
        self.StringGrid1.SetCell(0, 0, "姓名")  # 第一列标题
        self.StringGrid1.SetCell(1, 0, "年龄")  # 第二列标题
        self.StringGrid1.SetCell(2, 0, "城市")  # 第三列标题

        # 添加一些示例数据
        self.StringGrid1.SetCell(0, 1, "Alice")  # 第一行第一列
        self.StringGrid1.SetCell(1, 1, "30")     # 第一行第二列
        self.StringGrid1.SetCell(2, 1, "北京")   # 第一行第三列

        self.StringGrid1.SetCell(0, 2, "Bob")    # 第二行第一列
        self.StringGrid1.SetCell(1, 2, "25")     # 第二行第二列
        self.StringGrid1.SetCell(2, 2, "上海")   # 第二行第三列

        # 获取选中的单元格的值
        row = self.StringGrid1.Row  # 获取当前选中的行
        col = self.StringGrid1.Col  # 获取当前选中的列
        cell_value = self.StringGrid1.GetCell(col, row)  # 获取单元格的值

        # 弹出消息框显示选中单元格的值
        ShowMessage(f"选中单元格的值: {cell_value}")  # 弹出消息框

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 445
  ClientWidth = 482
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object StringGrid1: TStringGrid
    Left = 16
    Top = 248
    Width = 288
    Height = 120
    Options = [goFixedVertLine, goFixedHorzLine, goVertLine, goHorzLine, goRangeSelect, goEditing, goFixedRowDefAlign]
    TabOrder = 0
  end
  object Button1: TButton
    Left = 16
    Top = 384
    Width = 131
    Height = 25
    Caption = #26174#31034#36873#20013#21333#20803#26684
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

网格组件演示

使用的组件

  • Form1 (TForm)
  • StringGrid1 (TStringGrid)
  • Button1 (TButton)

技术特点

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

窗体属性

  • caption: #26174#31034#36873#20013#21333#20803#26684
  • height: 445
  • width: 482

代码分析

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

定义的类: - Form1

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

事件绑定: - self.Button1.OnClick = self.Button1Click - self.show_button.OnClick = self.on_show_button_click # 设置点击事件处理函数