Skip to content

TColorDialogDemo 项目分析

项目信息

文件结构

TColorDialogDemo/
├── 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 *

class Form1(Form):

    def __init__(self, owner):
        self.Button1 = Button(self)
        self.Memo1 = Memo(self)
        self.ColorDialog1 = ColorDialog(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建一个编辑框用于显示所选颜色
        self.color_edit = Edit(self)
        self.color_edit.Parent = self  # 设置父控件为窗体
        self.color_edit.Text = "Sample Text"  # 文本内容
        self.color_edit.SetBounds(150, 50, 200, 30)  # 设置编辑框的位置和大小

        # 创建一个按钮用于打开颜色选择对话框
        self.color_button = Button(self)
        self.color_button.Parent = self  # 设置父控件为窗体
        self.color_button.Caption = "Choose Color"  # 按钮上的标题
        self.color_button.SetBounds(150, 100, 150, 30)  # 设置按钮的位置和大小

        # 为按钮设置点击事件处理程序
        self.color_button.OnClick = self.choose_color

    def choose_color(self, Sender):
        # 创建颜色选择对话框
        color_dialog = ColorDialog(self)

        # 显示对话框并检查用户是否点击了“确定”
        if color_dialog.Execute():
            # 获取用户选择的颜色
            selected_color = color_dialog.Color

            # 将所选颜色应用于标签的背景色
            self.color_edit.Color = selected_color

            # 显示所选颜色的信息
            ShowMessage(f"Selected Color: {selected_color}")

    def Button1Click(self, Sender):
        if self.ColorDialog1.Execute():
            self.Memo1.Color = self.ColorDialog1.Color

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 334
  ClientWidth = 382
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Memo1: TMemo
    Left = 8
    Top = 168
    Width = 361
    Height = 89
    Lines.Strings = (
      'Memo1')
    TabOrder = 0
  end
  object Button1: TButton
    Left = 294
    Top = 264
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object ColorDialog1: TColorDialog
    Left = 256
    Top = 264
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

对话框组件演示

使用的组件

  • Form1 (TForm)
  • Memo1 (TMemo)
  • Button1 (TButton)
  • ColorDialog1 (TColorDialog)

技术特点

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

窗体属性

  • caption: Button1
  • height: 334
  • width: 382

代码分析

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

定义的类: - Form1

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

事件绑定: - self.Button1.OnClick = self.Button1Click - self.color_button.OnClick = self.choose_color