TFontDialogDemo 项目分析¶
项目信息¶
- 项目名称: TFontDialogDemo
- 下载链接: TFontDialogDemo.rar
- 分析时间: 2026-03-05
文件结构¶
TFontDialogDemo/
├── 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.FontDialog1 = FontDialog(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "Unit1.pydfm"))
self.Button1.OnClick = self.Button1Click
# 创建一个标签用于显示所选字体
self.font_label = Label(self)
self.font_label.Parent = self # 设置父控件为窗体
self.font_label.Caption = "Sample Text" # 标签上的标题
self.font_label.SetBounds(150, 50, 200, 30) # 设置标签的位置和大小
# 创建一个按钮用于打开字体选择对话框
self.font_button = Button(self)
self.font_button.Parent = self # 设置父控件为窗体
self.font_button.Caption = "Choose Font" # 按钮上的标题
self.font_button.SetBounds(150, 100, 150, 30) # 设置按钮的位置和大小
# 为按钮设置点击事件处理程序
self.font_button.OnClick = self.choose_font
def choose_font(self, Sender):
# 创建字体选择对话框
font_dialog = FontDialog(self)
# 显示对话框并检查用户是否点击了“确定”
if font_dialog.Execute():
# 获取用户选择的字体
selected_font = font_dialog.Font
# 将所选字体应用于标签
self.font_label.Font.Assign(selected_font)
# 显示所选字体的信息
ShowMessage(f"Selected Font: {selected_font.Name}, Size: {selected_font.Size}, Style: {selected_font.Style}")
def Button1Click(self, Sender):
if self.FontDialog1.Execute():
self.Memo1.Font.Assign(self.FontDialog1.Font)
设计文件: Unit1.sct¶
def Button1Click(Sender):
设计文件: Unit1.sfm¶
object Form1: TForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 267
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 = 104
Width = 369
Height = 121
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 = 0
end
object Button1: TButton
Left = 302
Top = 232
Width = 75
Height = 25
Caption = 'Button1'
TabOrder = 1
OnClick = Button1Click
end
object FontDialog1: TFontDialog
Font.Charset = DEFAULT_CHARSET
Font.Color = clWindowText
Font.Height = -15
Font.Name = 'Segoe UI'
Font.Style = []
Left = 264
Top = 232
end
end
其他文件¶
- Extractor_Icon.ico
- Project1.xml
详细分析¶
功能概述¶
对话框组件演示
使用的组件¶
- Form1 (TForm)
- Memo1 (TMemo)
- Button1 (TButton)
- FontDialog1 (TFontDialog)
技术特点¶
- 包含4个GUI组件
- 定义2个事件处理
窗体属性¶
- caption: Button1
- height: 267
- width: 382
代码分析¶
导入的模块: - import os - from glcl import *
定义的类: - Form1
定义的方法: - init - choose_font - Button1Click
事件绑定: - self.Button1.OnClick = self.Button1Click - self.font_button.OnClick = self.choose_font