TFileSaveDialogDemo 项目分析¶
项目信息¶
- 项目名称: TFileSaveDialogDemo
- 下载链接: TFileSaveDialogDemo.rar
- 分析时间: 2026-03-05
文件结构¶
TFileSaveDialogDemo/
├── 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.Memo1 = Memo(self)
self.Button1 = Button(self)
self.FileSaveDialog1 = FileSaveDialog(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 = 'Save File' # 按钮标题
self.button.Left = 150 # 设置按钮的左侧位置
self.button.Top = 50 # 设置按钮的顶部位置
self.button.OnClick = self.save_file_dialog # 设置按钮点击事件处理程序
# 创建一个标签用于显示所选文件的路径
self.label = Label(self)
self.label.Parent = self # 设置父控件为当前窗体
self.label.Left = 10 # 设置标签的左侧位置
self.label.Top = self.button.Top + 30 # 设置标签的顶部位置,位于按钮下方
self.label.Caption = 'Saved File: ' # 标签标题
# 打开文件保存对话框的处理函数
def save_file_dialog(self, Sender):
# 创建文件保存对话框实例
dialog = FileSaveDialog(self)
dialog.Title = 'Save a File' # 对话框标题
# 显示对话框并检查用户是否指定了文件名
if dialog.Execute():
saved_file = dialog.FileName # 获取所选保存文件的路径
self.label.Caption = f'Saved File: {saved_file}' # 更新标签内容
def Button1Click(self, Sender):
if self.FileSaveDialog1.Execute():
self.Memo1.Lines.SaveToFile(self.FileSaveDialog1.FileName)
设计文件: Unit1.sct¶
def Button1Click(Sender):
设计文件: Unit1.sfm¶
object Form1: TForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 265
ClientWidth = 382
Color = clBtnFace
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = 'Segoe UI'
Font.Style = []
TextHeight = 20
object Button1: TButton
Left = 294
Top = 224
Width = 75
Height = 25
Caption = #20445#23384#25991#20214
TabOrder = 0
OnClick = Button1Click
end
object Memo1: TMemo
Left = 16
Top = 128
Width = 353
Height = 89
Lines.Strings = (
'*** Python 3.12.4 (heads/master-dirty:9c62cab, Jun 9 '
'2024, 14:50:38) [MSC v.1940 64 bit (AMD64)] on '
'win32. ***'
'*** '#36828#31243' Python '#24341#25806#21551#21160' ***')
TabOrder = 1
end
object FileSaveDialog1: TFileSaveDialog
FavoriteLinks = <>
FileTypes = <>
Options = []
Left = 256
Top = 224
end
end
其他文件¶
- Extractor_Icon.ico
- Project1.xml
详细分析¶
功能概述¶
对话框组件演示
使用的组件¶
- Form1 (TForm)
- Button1 (TButton)
- Memo1 (TMemo)
- FileSaveDialog1 (TFileSaveDialog)
技术特点¶
- 包含4个GUI组件
- 定义2个事件处理
窗体属性¶
- caption: #20445#23384#25991#20214
- height: 265
- width: 382
代码分析¶
导入的模块: - import os - from glcl import *
定义的类: - Form1
定义的方法: - init - save_file_dialog - Button1Click
事件绑定: - self.Button1.OnClick = self.Button1Click - self.button.OnClick = self.save_file_dialog # 设置按钮点击事件处理程序