Skip to content

TOpenPictureDialogDemo 项目分析

项目信息

文件结构

TOpenPictureDialogDemo/
├── 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.Image1 = Image(self)
        self.OpenPictureDialog1 = OpenPictureDialog(self)
        self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
        self.Button1.OnClick = self.Button1Click

        # 创建一个按钮,点击后打开图片选择对话框
        self.button = Button(self)
        self.button.Parent = self   # 设置父控件为窗体
        self.button.Caption = 'Open Picture'    # 标题
        self.button.OnClick = self.open_picture_dialog  # 设置按钮点击事件
        self.button.SetBounds(50, 50, 150, 30)  # 设置按钮位置和大小

    def open_picture_dialog(self, Sender):
        # 创建一个图片选择对话框实例
        dialog = OpenPictureDialog(self)

        # 设置对话框的选项,例如文件过滤器
        dialog.Filter = 'Image Files|*.bmp;*.jpg;*.jpeg;*.png;*.gif'
        dialog.Title = 'Select an Image'  # 设置对话框标题

        # 显示对话框并检查用户是否点击了“确定”
        if dialog.Execute():
            # 获取选择的文件名
            file_name = dialog.FileName

            # 在这里可以使用选择的文件名,比如加载图片等
            ShowMessage(f'Selected file: {file_name}')
            # 例如,可以使用 TImage 控件来显示图片,但这里仅打印文件名

    def Button1Click(self, Sender):
        if self.OpenPictureDialog1.Execute():
            self.Image1.Picture.LoadFromFile(self.OpenPictureDialog1.FileName)

设计文件: Unit1.sct

def Button1Click(Sender): 

设计文件: Unit1.sfm

object Form1: TForm
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 342
  ClientWidth = 403
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -15
  Font.Name = 'Segoe UI'
  Font.Style = []
  TextHeight = 20
  object Image1: TImage
    Left = 8
    Top = 16
    Width = 385
    Height = 281
  end
  object Button1: TButton
    Left = 318
    Top = 304
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 0
    OnClick = Button1Click
  end
  object OpenPictureDialog1: TOpenPictureDialog
    Left = 344
    Top = 269
  end
end

其他文件

  • Extractor_Icon.ico
  • Project1.xml

详细分析

功能概述

对话框组件演示

使用的组件

  • Form1 (TForm)
  • Image1 (TImage)
  • Button1 (TButton)
  • OpenPictureDialog1 (TOpenPictureDialog)

技术特点

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

窗体属性

  • caption: Button1
  • height: 342
  • width: 403

代码分析

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

定义的类: - Form1

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

事件绑定: - self.Button1.OnClick = self.Button1Click - self.button.OnClick = self.open_picture_dialog # 设置按钮点击事件