Skip to content

TTimerDemo 项目分析

项目信息

  • 项目名称: TTimerDemo
  • 下载链接: TTimerDemo.rar
  • 分析时间: 2026-03-05

文件结构

TTimerDemo/
├── 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 time

class Form1(Form):

    def __init__(self, owner):
        self.Timer1 = Timer(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Timer1.OnTimer = self.Timer1Timer
        self.Timer1.Enabled = True

        # 创建 TLabel 控件用于显示当前时间
        self.label = Label(self)
        self.label.SetBounds(50, 50, 200, 30)  # 设置标签的位置和大小
        self.label.Parent = self  # 将标签设置为窗体的子控件
        self.label.Caption = "当前时间:"  # 标签初始文本

        # 创建 TTimer 控件
        self.timer = Timer(self)
        self.timer.Interval = 1000  # 设置定时器的间隔为 1000 毫秒(1 秒)
        self.timer.OnTimer = self.on_timer  # 连接定时器事件到处理函数

        self.timer.Enabled = True  # 启动定时器

    # 定义定时器触发时执行的操作
    def on_timer(self, Sender):
        timestamp = time.time()
        current_time = time.ctime(timestamp)
        #current_time = time.localtime()
        self.label.Caption = current_time  # 更新标签文本显示当前时间

    def Timer1Timer(self, Sender):
        self.Caption = str(self.Tag)
        self.Tag += 1

设计文件: Unit1.sct

def Timer1Timer(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 133
  ClientWidth = 256
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Timer1: TTimer
    Enabled = False
    OnTimer = Timer1Timer
    Left = 192
    Top = 72
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • Timer1 (TTimer)

技术特点

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

窗体属性

  • caption: Form1
  • height: 133
  • width: 256

代码分析

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

定义的类: - Form1

定义的方法: - init - on_timer - Timer1Timer