Skip to content

TTimePickerDemo 项目分析

项目信息

文件结构

TTimePickerDemo/
├── 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 *
# 需要导入datetime包
from datetime import datetime

class Form1(Form):

    def __init__(self, owner):
        self.Button1 = Button(self)
        self.TimePicker1 = TimePicker(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click
        self.TimePicker1.OnChange = self.TimePicker1Change

        # 创建 TTimePicker 组件
        self.time_picker = TimePicker(self)
        self.time_picker.Parent = self      # 设置父控件为窗体
        self.time_picker.Left = 10          # 设置左边距离
        self.time_picker.Top = 10           # 设置顶部距离

        # 创建一个按钮,用于获取选择的时间
        self.show_time_button = Button(self)
        self.show_time_button.Parent = self # 设置父控件为窗体
        self.show_time_button.Left = 10     # 设置左边距离
        self.show_time_button.Top = 60      # 设置顶部距离
        self.show_time_button.Width = 200   # 设置宽度
        self.show_time_button.Caption = "显示选择的时间"  # 按钮标题
        self.show_time_button.OnClick = self.show_selected_time  # 绑定点击事件

        # 创建一个标签,用于显示时间
        self.selected_time_label = Label(self)
        self.selected_time_label.Parent = self  # 设置父控件为窗体
        self.selected_time_label.Left = 10      # 设置左边距离
        self.selected_time_label.Top = 100      # 设置顶部距离
        self.selected_time_label.Caption = "选择的时间将在这里显示"  # 设置化标签标题

    # 显示选择的时间的事件处理函数
    def show_selected_time(self, Sender):
        selected_time = self.time_picker.Time  # 获取选中的时间
        self.selected_time_label.Caption = f"选择的时间: {selected_time}"  # 更新标签显示所选时间

    def TimePicker1Change(self, Sender):
        self.Caption = self.TimePicker1.Time

    def Button1Click(self, Sender):
        # 设置TimePicker1的当前时间
        self.TimePicker1.Time = datetime.now()

设计文件: Unit1.sct

def TimePicker1Change(Sender): 
def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 205
  ClientWidth = 382
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object TimePicker1: TTimePicker
    Left = 16
    Top = 136
    Width = 106
    Font.Charset = DEFAULT_CHARSET
    Font.Color = clWindowText
    Font.Height = -20
    Font.Name = 'Segoe UI'
    Font.Style = []
    TabOrder = 0
    Time = 45567.04596747685000000
    TimeFormat = 'h:nn'
    OnChange = TimePicker1Change
  end
  object Button1: TButton
    Left = 136
    Top = 136
    Width = 147
    Height = 25
    Caption = #35774#32622#20026#24403#21069#31995#32479#26102#38388
    TabOrder = 1
    OnClick = Button1Click
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

图像处理组件演示

使用的组件

  • Form1 (TForm)
  • TimePicker1 (TTimePicker)
  • Button1 (TButton)

技术特点

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

窗体属性

  • caption: #35774#32622#20026#24403#21069#31995#32479#26102#38388
  • height: 205
  • width: 382

代码分析

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

定义的类: - Form1

定义的方法: - init - show_selected_time - TimePicker1Change - Button1Click

事件绑定: - self.Button1.OnClick = self.Button1Click - self.TimePicker1.OnChange = self.TimePicker1Change - self.show_time_button.OnClick = self.show_selected_time # 绑定点击事件