Skip to content

TMainMenuDemo 项目分析

项目信息

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

文件结构

TMainMenuDemo/
├── Extractor_Icon.ico
├── Project1.py
├── Project1.xml
├── Unit1.py
├── Unit1.pydfm
├── Unit1.sct
├── Unit1.sfm
├── Unit2.py
├── Unit2.pydfm
├── Unit2.sct
├── Unit2.sfm

主程序文件: Project1.py

# 从 glcl 模块导入所有内容。
from glcl import *

# 从 Unit1 模块导入所有内容。Unit1 通常包含定义了窗体的类,比如 Form1。
from Unit1 import *

def main():
    # 初始化应用程序。这个调用会设置应用程序的基本环境。
    Application.Initialize()

    # 设置应用程序的标题。这个标题会显示在任务栏或窗口的标题栏上。
    Application.Title = 'Project1'

    # 确保应用程序的主窗体在任务栏上显示。这个设置对 Windows 平台特别有效。
    Application.MainFormOnTaskbar = True

    # 加载应用程序的图标。图标文件的路径是相对于当前脚本文件所在目录的。
    # os.path.join 函数用于拼接路径,os.path.dirname 和 os.path.abspath 函数用于获取脚本的目录。
    Application.Icon.LoadFromFile(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Extractor_Icon.ico"))

    # 创建一个 Form1 类的实例,并将其作为主窗体传递给 Application 对象。
    MainForm = Form1(Application)

    # 显示主窗体。这个调用会使主窗体可见。
    MainForm.Show()

    # 释放控制台。如果脚本是在一个有控制台的环境中运行,这行代码会关闭控制台窗口。
    FreeConsole()

    # 启动应用程序的主事件循环。这个循环会处理用户输入和其他事件,直到应用程序退出。
    Application.Run()

    # 当应用程序的主事件循环结束后,释放主窗体占用的资源。
    MainForm.Free()

# 检查脚本是否作为主程序运行。如果是,调用 main 函数。
# 这个检查确保只有在脚本直接运行时才会执行 main 函数,而在被导入为模块时不会执行。
if __name__ == '__main__':
    main()

单元文件: Unit1.py

import os
from glcl import *
from Unit2 import *

# 定义 Form1 类,继承自 Form 类。
class Form1(Form):

    # 构造函数,初始化窗体和控件。
    def __init__(self, owner):
        # 创建一个 Button 控件,并将其添加到当前窗体。
        self.Button1 = Button(self)
        # 创建一个 MainMenu 控件,并将其添加到当前窗体。
        self.MainMenu1 = MainMenu(self)
        # 加载窗体属性,从指定的文件路径加载设置。
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        # 设置 MenuItem10 的点击事件处理函数。
        self.MenuItem10.OnClick = self.MenuItem10Click
        # 设置 Button1 的点击事件处理函数。
        self.Button1.OnClick = self.Button1Click

    # Button1 的点击事件处理函数,创建并显示 Form2 实例。
    def Button1Click(self, Sender):
        frm2 = Form2(self)
        frm2.Show()

    # MenuItem10 的点击事件处理函数,显示消息框。
    def MenuItem10Click(self, Sender):
        ShowMessage('控件方式创建菜单')

单元文件: Unit2.py

import os
from glcl import *

class Form2(Form):

    def __init__(self, owner):
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit2.pydfm"))

        self.Caption = 'Main Form with TMainMenu'

        # 创建主菜单
        self.MainMenu = MainMenu(self)

        # 创建文件菜单项
        file_menu = MenuItem(self.MainMenu)
        file_menu.Caption = 'File'
        self.MainMenu.Items.Add(file_menu)

        # 创建打开菜单项
        open_item = MenuItem(self.MainMenu)
        open_item.Caption = 'Open'
        open_item.OnClick = self.open_file
        file_menu.Add(open_item)

        # 创建退出菜单项
        exit_item = MenuItem(self.MainMenu)
        exit_item.Caption = 'Exit'
        exit_item.OnClick = self.exit_app
        file_menu.Add(exit_item)

        # 打开文件的事件处理函数
    def open_file(self, Sender):
        print('Open menu item clicked')
        ShowMessage('代码方式创建菜单')

    # 退出应用的事件处理函数
    def exit_app(self, Sender):
        Application.Terminate()

设计文件: Unit1.sct

def Button1Click(Sender): 
def MenuItem10Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 91
  ClientWidth = 327
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  Menu = MainMenu1
  TextHeight = 20
  object Button1: TButton
    Left = 88
    Top = 24
    Width = 163
    Height = 25
    Caption = #20195#30721#26041#24335#21019#24314#33756#21333
    TabOrder = 0
    OnClick = Button1Click
  end
  object MainMenu1: TMainMenu
    Left = 24
    Top = 24
    object MenuItem3: TMenuItem
      Caption = 'MenuItem3'
      object MenuItem10: TMenuItem
        Caption = #26174#31034#28040#24687
        OnClick = MenuItem10Click
      end
    end
    object MenuItem2: TMenuItem
      Caption = 'MenuItem2'
      object MenuItem9: TMenuItem
        Caption = 'MenuItem9'
      end
    end
    object MenuItem1: TMenuItem
      Caption = 'MenuItem1'
      object MenuItem4: TMenuItem
        Caption = 'MenuItem4'
      end
      object MenuItem5: TMenuItem
        Caption = 'MenuItem5'
        object MenuItem7: TMenuItem
          Caption = 'MenuItem7'
        end
        object MenuItem8: TMenuItem
          Caption = 'MenuItem8'
        end
      end
      object MenuItem6: TMenuItem
        Caption = 'MenuItem6'
      end
    end
  end
end

设计文件: Unit2.sct


设计文件: Unit2.sfm

object Form2: TForm
  Left = 0
  Top = 0
  Caption = 'Form2'
  ClientHeight = 100
  ClientWidth = 307
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

GUI组件演示

使用的组件

  • Form1 (TForm)
  • Button1 (TButton)
  • MainMenu1 (TMainMenu)
  • MenuItem3 (TMenuItem)
  • MenuItem10 (TMenuItem)
  • MenuItem2 (TMenuItem)
  • MenuItem9 (TMenuItem)
  • MenuItem1 (TMenuItem)
  • MenuItem4 (TMenuItem)
  • MenuItem5 (TMenuItem)
  • MenuItem7 (TMenuItem)
  • MenuItem8 (TMenuItem)
  • MenuItem6 (TMenuItem)

技术特点

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

窗体属性

  • caption: MenuItem6
  • height: 91
  • width: 327

代码分析

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

定义的类: - Form1

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

事件绑定: - self.MenuItem10.OnClick = self.MenuItem10Click - self.Button1.OnClick = self.Button1Click