Skip to content

TLinkLabelDemo 项目分析

项目信息

文件结构

TLinkLabelDemo/
├── 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 *
import webbrowser

class Form1(Form):

    def __init__(self, owner):
        self.LinkLabel1 = LinkLabel(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.LinkLabel1.OnLinkClick = self.LinkLabel1LinkClick
        self.LinkLabel1.Caption = '<a href="https://forum.glsite.com/">论坛</a>'

        # 创建链接标签控件
        self.link_label = LinkLabel(self)  # 创建 TLinkLabel 控件
        self.link_label.Parent = self  # 设置父控件
        self.link_label.Caption = '<a>Click here</a> to glsite.com!'  # 设置链接文本
        self.link_label.Left = 50  # 设置链接标签的左边距
        self.link_label.Top = 80  # 设置链接标签的顶部边距
        self.link_label.Font.Size = 12  # 设置字体大小
        self.link_label.Font.Color = clBlue  # 设置字体颜色为蓝色
        self.link_label.OnClick = self.link_label_click  # 绑定点击事件

    def link_label_click(self, Sender):
        # 点击链接标签时调用的事件处理程序
        webbrowser.open('http://www.glsite.com')

    def LinkLabel1LinkClick(self, Sender, Link, LinkType):
        webbrowser.open(Link)
        ShowMessage(Link)

设计文件: Unit1.sct

def LinkLabel1LinkClick(Sender, Link, LinkType): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 166
  ClientWidth = 285
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object LinkLabel1: TLinkLabel
    Left = 32
    Top = 112
    Width = 74
    Height = 24
    Caption = 'LinkLabel1'
    TabOrder = 0
    OnLinkClick = LinkLabel1LinkClick
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • LinkLabel1 (TLinkLabel)

技术特点

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

窗体属性

  • caption: LinkLabel1
  • height: 166
  • width: 285

代码分析

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

定义的类: - Form1

定义的方法: - init - link_label_click - LinkLabel1LinkClick

事件绑定: - self.link_label.OnClick = self.link_label_click # 绑定点击事件