TCalendarViewDemo 项目分析¶
项目信息¶
- 项目名称: TCalendarViewDemo
- 下载链接: TCalendarViewDemo.rar
- 分析时间: 2026-03-05
文件结构¶
TCalendarViewDemo/
├── 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 *
from datetime import datetime
class Form1(Form):
def __init__(self, owner):
self.CalendarView1 = CalendarView(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
self.CalendarView1.OnChange = self.CalendarView1Change
# 创建 TCalendarView 组件
self.calendar_view = CalendarView(self)
self.calendar_view.Parent = self # 设置父控件为窗体
self.calendar_view.Left = 10 # 设置左边距离
self.calendar_view.Top = 50 # 设置顶边距离
# 创建一个按钮,用于获取选择的日期
self.show_date_button = Button(self)
self.show_date_button.Parent = self # 设置父控件为窗体
self.show_date_button.Caption = "显示选择的日期" # 设置标题
self.show_date_button.Left = 10 # 设置左边距离
self.show_date_button.Top = 500 # 设置顶边距离
self.show_date_button.Width = 150 # 设置宽度
self.show_date_button.OnClick = self.show_selected_date # 绑定点击事件
# 创建一个标签,用于显示日期
self.selected_date_label = Label(self)
self.selected_date_label.Parent = self # 设置父控件为窗体
self.selected_date_label.Left = 10 # 设置左边距离
self.selected_date_label.Top = 10 # 设置顶边距离
self.selected_date_label.Caption = "选择的日期将在这里显示" # 设置标题
# 显示选择的日期的事件处理函数
def show_selected_date(self, Sender):
try:
selected_date = self.calendar_view.Date # 获取选中的日期
self.selected_date_label.Caption = f"选择的日期: {selected_date}" # 更新显示
except:
ShowMessage('未选中日期')
def CalendarView1Change(self, Sender):
try:
selected_date = self.CalendarView1.Date # 获取选中的日期
self.Caption = f"选择的日期: {selected_date}" # 更新显示
except:
ShowMessage('未选中日期')
设计文件: Unit1.sct¶
def CalendarView1Change(Sender):
设计文件: Unit1.sfm¶
object Form1: TForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 539
ClientWidth = 638
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = 'Segoe UI'
Font.Style = []
TextHeight = 20
object CalendarView1: TCalendarView
Left = 336
Top = 200
Date = 45567.00000000000000000
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -25
Font.Name = 'Segoe UI'
Font.Style = []
HeaderInfo.DaysOfWeekFont.Charset = DEFAULT_CHARSET
HeaderInfo.DaysOfWeekFont.Color = clWindowText
HeaderInfo.DaysOfWeekFont.Height = -17
HeaderInfo.DaysOfWeekFont.Name = 'Segoe UI'
HeaderInfo.DaysOfWeekFont.Style = []
HeaderInfo.Font.Charset = DEFAULT_CHARSET
HeaderInfo.Font.Color = clWindowText
HeaderInfo.Font.Height = -25
HeaderInfo.Font.Name = 'Segoe UI'
HeaderInfo.Font.Style = []
OnChange = CalendarView1Change
ParentFont = False
TabOrder = 0
end
end
其他文件¶
- Extractor_Icon.ico
- Project1.xml
详细分析¶
功能概述¶
GUI组件演示
使用的组件¶
- Form1 (TForm)
- CalendarView1 (TCalendarView)
技术特点¶
- 包含2个GUI组件
- 定义2个事件处理
窗体属性¶
- caption: Form1
- height: 539
- width: 638
代码分析¶
导入的模块: - import os - from glcl import * - from datetime import datetime
定义的类: - Form1
定义的方法: - init - show_selected_date - CalendarView1Change
事件绑定: - self.CalendarView1.OnChange = self.CalendarView1Change - self.show_date_button.OnClick = self.show_selected_date # 绑定点击事件