PraButtonStyleDemo 项目分析¶
项目信息¶
- 项目名称: PraButtonStyleDemo
- 下载链接: PraButtonStyleDemo.rar
- 分析时间: 2026-03-31
项目概述¶
PraButtonStyleDemo是一个展示自定义按钮样式组件的演示项目,包含6个不同的演示示例,展示了PraButtonStyle组件的各种功能和用法。该项目演示了如何创建具有现代UI风格的按钮,包括图标按钮、导航菜单按钮、样式切换按钮等。
文件结构¶
PraButtonStyleDemo/
├── demo1/ # 演示1:基本图标按钮
│ ├── img/ # 图标资源目录
│ │ ├── content-save (1).png # 保存图标变体1
│ │ ├── content-save (5).png # 保存图标变体5
│ │ ├── content-save (6).png # 保存图标变体6
│ │ ├── content-save.png # 保存图标
│ │ ├── delete (1).png # 删除图标变体1
│ │ ├── delete.png # 删除图标
│ │ ├── facebook (1).png # Facebook图标变体1
│ │ ├── facebook (2).png # Facebook图标变体2
│ │ ├── facebook.png # Facebook图标
│ │ ├── linkedin (1).png # LinkedIn图标变体1
│ │ ├── linkedin.png # LinkedIn图标
│ │ ├── pencil (1).png # 编辑图标变体1
│ │ ├── pencil.png # 编辑图标
│ │ ├── twitter (1).png # Twitter图标变体1
│ │ ├── twitter.png # Twitter图标
│ │ ├── youtube (1).png # YouTube图标变体1
│ │ └── youtube.png # YouTube图标
│ ├── Extractor_Icon.ico # 应用程序图标
│ ├── main.py # 主窗体代码
│ ├── main.pydfm # 窗体设计文件
│ ├── main.sct # 脚本文件
│ ├── main.sfm # 窗体结构文件
│ ├── Project1.py # 项目主程序
│ └── Project1.xml # 项目配置文件
├── demo2/ # 演示2:侧边导航菜单
│ ├── Extractor_Icon.ico # 应用程序图标
│ ├── main.py # 主窗体代码
│ ├── main.pydfm # 窗体设计文件
│ ├── main.sct # 脚本文件
│ ├── main.sfm # 窗体结构文件
│ ├── Project1.py # 项目主程序
│ └── Project1.xml # 项目配置文件
├── demo3/ # 演示3:按钮样式控制
│ ├── Extractor_Icon.ico # 应用程序图标
│ ├── main.py # 主窗体代码
│ ├── main.pydfm # 窗体设计文件
│ ├── main.sct # 脚本文件
│ ├── main.sfm # 窗体结构文件
│ ├── Project1.py # 项目主程序
│ └── Project1.xml # 项目配置文件
├── demo4/ # 演示4:简单按钮展示
│ ├── Extractor_Icon.ico # 应用程序图标
│ ├── main.py # 主窗体代码
│ ├── main.pydfm # 窗体设计文件
│ ├── main.sct # 脚本文件
│ ├── main.sfm # 窗体结构文件
│ ├── Project1.py # 项目主程序
│ └── Project1.xml # 项目配置文件
├── demo5/ # 演示5:复杂按钮布局
│ ├── Extractor_Icon.ico # 应用程序图标
│ ├── main.py # 主窗体代码
│ ├── main.pydfm # 窗体设计文件
│ ├── main.sct # 脚本文件
│ ├── main.sfm # 窗体结构文件
│ ├── Project1.py # 项目主程序
│ └── Project1.xml # 项目配置文件
└── demo6/ # 演示6:图片轮播按钮
├── Extractor_Icon.ico # 应用程序图标
├── main.py # 主窗体代码
├── main.pydfm # 窗体设计文件
├── main.sct # 脚本文件
├── main.sfm # 窗体结构文件
├── Project1.py # 项目主程序
└── Project1.xml # 项目配置文件
项目主程序文件: Project1.py (各demo相同)¶
from glcl import *
from main 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) # 或 uMain、Form13等,根据具体demo
MainForm.Show()
FreeConsole()
Application.Run()
MainForm.Free()
if __name__ == '__main__':
main()
各演示示例详细分析¶
演示1: demo1 - 基本图标按钮¶
文件: demo1/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
import os
from glcl import *
class uMain(Form):
def __init__(self, owner):
self.Panel1 = Panel(self)
self.PraButtonStyle1 = PraButtonStyle(self)
self.PraButtonStyle2 = PraButtonStyle(self)
self.PraButtonStyle3 = PraButtonStyle(self)
self.PraButtonStyle4 = PraButtonStyle(self)
self.PraButtonStyle5 = PraButtonStyle(self)
self.PraButtonStyle6 = PraButtonStyle(self)
self.PraButtonStyle7 = PraButtonStyle(self)
self.PraButtonStyle8 = PraButtonStyle(self)
self.PraButtonStyle9 = PraButtonStyle(self)
self.PraButtonStyle10 = PraButtonStyle(self)
self.PraButtonStyle11 = PraButtonStyle(self)
self.PraButtonStyle12 = PraButtonStyle(self)
self.PraButtonStyle13 = PraButtonStyle(self)
self.PraButtonStyle14 = PraButtonStyle(self)
self.Label1 = Label(self)
self.PraButtonStyle16 = PraButtonStyle(self)
self.PraButtonStyle15 = PraButtonStyle(self)
self.PraButtonStyle17 = PraButtonStyle(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
功能特点: - 展示17个不同样式的PraButtonStyle按钮 - 每个按钮都配置了不同的图标和样式 - 包含社交媒体图标(Facebook、Twitter、YouTube、LinkedIn) - 包含功能图标(保存、删除、编辑) - 演示按钮的基本布局和外观
演示2: demo2 - 侧边导航菜单¶
文件: demo2/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
# 导入操作系统模块,用于文件和路径操作
import os
# 从glcl库导入所有组件(这是一个GUI框架)
from glcl import *
class Form1(Form):
"""
主窗体类,继承自Form基类
实现了一个带有侧边导航菜单的界面
"""
def __init__(self, owner):
"""
构造函数,初始化窗体及其所有控件
参数:
owner: 窗体的所有者(父窗体)
"""
# 分割视图控件,用于实现可折叠的侧边菜单
self.svMenu = SplitView(self)
# 多个面板容器,用于布局和组织其他控件
self.Panel1 = Panel(self) # 主面板1
self.Panel2 = Panel(self) # 主面板2
self.Panel3 = Panel(self) # 主面板3
self.Panel4 = Panel(self) # 主面板4(用于菜单头部)
# 形状控件,用于装饰或分隔界面元素
self.Shape2 = Shape(self)
self.Shape3 = Shape(self)
# 多个自定义按钮样式控件(具有特殊样式的按钮)
self.PraButtonStyle1 = PraButtonStyle(self)
self.PraButtonStyle3 = PraButtonStyle(self)
self.PraButtonStyle4 = PraButtonStyle(self)
self.PraButtonStyle2 = PraButtonStyle(self)
self.PraButtonStyle5 = PraButtonStyle(self)
self.PraButtonStyle6 = PraButtonStyle(self)
self.PraButtonStyle7 = PraButtonStyle(self) # 菜单展开/折叠按钮
self.PraButtonStyle8 = PraButtonStyle(self) # 关闭窗体按钮
# 用于显示图标或背景图
self.Image1 = Image(self)
# 用于显示文本
self.Label1 = Label(self)
# 加载窗体的布局文件(.pydfm格式)
# 使用__file__获取当前脚本的路径,构建完整的布局文件路径
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
# 绑定按钮点击事件
self.PraButtonStyle7.OnClick = self.PraButtonStyle7Click # 菜单按钮点击事件
self.PraButtonStyle8.OnClick = self.PraButtonStyle8Click # 关闭按钮点击事件
# 设置窗体的初始位置(左上角坐标)
self.Left = 0
self.Top = 0
def UpdateShowCaption(self, AShowCaption):
"""
更新侧边菜单中所有按钮的标题显示状态
参数:
AShowCaption: 布尔值,True表示显示按钮标题,False表示隐藏
"""
# 遍历分割视图中的所有控件
for i in range(0, self.svMenu.ControlCount):
# 检查控件是否为PraButtonStyle类型(自定义按钮)
if isinstance(self.svMenu.Controls[i], PraButtonStyle):
# 设置按钮的标题显示状态
self.svMenu.Controls[i].ShowCaption = AShowCaption
def PraButtonStyle8Click(self, Sender):
"""
关闭按钮点击事件处理函数
点击时关闭当前窗体
参数:
Sender: 触发事件的控件对象
"""
self.Close()
def PraButtonStyle7Click(self, Sender):
"""
菜单按钮点击事件处理函数
点击时展开或折叠侧边菜单,并调整相关控件的大小和显示状态
参数:
Sender: 触发事件的控件对象
"""
if self.svMenu.Opened:
# 如果菜单当前是展开状态,则关闭菜单
self.svMenu.Close()
# 将菜单容器宽度设置为60(折叠状态)
self.Panel4.Width = 60
# 隐藏标签(通常是菜单标题)
self.Label1.Visible = False
# 隐藏所有按钮的标题文本
self.UpdateShowCaption(False)
else:
# 如果菜单当前是折叠状态,则展开菜单
self.svMenu.Open()
# 将菜单容器宽度设置为204(展开状态)
self.Panel4.Width = 204
# 显示标签(通常是菜单标题)
self.Label1.Visible = True
# 显示所有按钮的标题文本
self.UpdateShowCaption(True)
功能特点: - 实现可折叠的侧边导航菜单 - 使用SplitView控件实现菜单展开/折叠动画 - 动态控制按钮标题的显示/隐藏 - 包含菜单头部和关闭按钮 - 演示PraButtonStyle在导航菜单中的应用
演示3: demo3 - 按钮样式控制¶
文件: demo3/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
# 导入操作系统模块,用于文件和路径操作
import os
# 从glcl库导入所有组件(这是一个GUI框架)
from glcl import *
class Form1(Form):
"""
主窗体类,继承自Form基类
实现了一个用于演示按钮样式设置和控制的界面
包含复选框、下拉框和自定义按钮,用于动态调整按钮的外观
"""
def __init__(self, owner):
"""
构造函数,初始化窗体及其所有控件
参数:
owner: 窗体的所有者(父窗体)
"""
# 两个复选框控件,用于控制按钮的状态
self.CheckBox1 = CheckBox(self) # 用于控制按钮的轮廓样式
self.CheckBox2 = CheckBox(self) # 用于控制按钮的启用/禁用状态
# 两个下拉框控件,用于选择按钮的样式
self.ComboBox1 = ComboBox(self) # 用于选择按钮的主题样式(Primary, Secondary等)
self.ComboBox2 = ComboBox(self) # 用于选择按钮的形状(圆角矩形或普通矩形)
# 标签控件,用于显示说明文字
self.Label1 = Label(self)
self.Label2 = Label(self)
# 自定义按钮控件,这是主要演示的对象
self.PraButtonStyle = PraButtonStyle(self)
# 加载窗体的布局文件(.pydfm格式)
# 使用__file__获取当前脚本的路径,构建完整的布局文件路径
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
# 绑定事件处理函数
self.CheckBox1.OnClick = self.CheckBox1Click # 轮廓样式复选框点击事件
self.CheckBox2.OnClick = self.CheckBox2Click # 启用状态复选框点击事件
self.ComboBox2.OnSelect = self.ComboBox2Select # 形状选择下拉框选择事件
# 定义按钮主题样式的数组,包含所有可用的样式类型
# 这些常量(bsPrimary, bsSecondary等)是从glcl库导入的
self.bsArr = [bsPrimary, bsSecondary, bsSuccess, bsDanger, bsWarning, bsInfo, bsLight, bsDark]
# 绑定主题样式选择下拉框的选择事件
self.ComboBox1.OnSelect = self.ComboBox1Select
def ComboBox1Select(self, Sender):
"""
主题样式选择下拉框的选择事件处理函数
根据用户选择的样式,改变按钮的主题样式和标题文字
参数:
Sender: 触发事件的控件对象(这里是ComboBox1)
"""
# 获取选中的项的索引(ItemIndex从0开始,减1是为了匹配bsArr数组的索引)
index = self.ComboBox1.ItemIndex - 1
# 根据选择的索引设置按钮的样式
if index == self.bsArr.index(bsPrimary):
# 设置为主样式(通常是蓝色)
self.PraButtonStyle.Style = bsPrimary
self.PraButtonStyle.Caption = 'Primary'
elif index == self.bsArr.index(bsSecondary):
# 设置为次级样式(通常是灰色)
self.PraButtonStyle.Style = bsSecondary
self.PraButtonStyle.Caption = 'Secondary'
elif index == self.bsArr.index(bsSuccess):
# 设置为成功样式(通常是绿色)
self.PraButtonStyle.Style = bsSuccess
self.PraButtonStyle.Caption = 'Success'
elif index == self.bsArr.index(bsDanger):
# 设置为危险样式(通常是红色)
self.PraButtonStyle.Style = bsDanger
self.PraButtonStyle.Caption = 'Danger'
elif index == self.bsArr.index(bsWarning):
# 设置为警告样式(通常是黄色)
self.PraButtonStyle.Style = bsWarning
self.PraButtonStyle.Caption = 'Warning'
elif index == self.bsArr.index(bsInfo):
# 设置为信息样式(通常是浅蓝色)
self.PraButtonStyle.Style = bsInfo
self.PraButtonStyle.Caption = 'Info'
elif index == self.bsArr.index(bsLight):
# 设置为亮色样式(通常是浅灰色)
self.PraButtonStyle.Style = bsLight
self.PraButtonStyle.Caption = 'Light'
elif index == self.bsArr.index(bsDark):
# 设置为暗色样式(通常是深灰色或黑色)
self.PraButtonStyle.Style = bsDark
self.PraButtonStyle.Caption = 'Dark'
def ComboBox2Select(self, Sender):
"""
形状选择下拉框的选择事件处理函数
根据用户选择的形状类型,改变按钮的形状
参数:
Sender: 触发事件的控件对象(这里是ComboBox2)
"""
if self.ComboBox2.ItemIndex == 0:
# 选择第一个选项:设置为圆角矩形
self.PraButtonStyle.Shape = stRoundRect
elif self.ComboBox2.ItemIndex == 1:
# 选择第二个选项:设置为普通矩形(直角)
self.PraButtonStyle.Shape = stRectangle
def CheckBox2Click(self, Sender):
"""
启用状态复选框的点击事件处理函数
控制按钮的启用/禁用状态
参数:
Sender: 触发事件的控件对象(这里是CheckBox2)
"""
# 将按钮的启用状态设置为复选框的选中状态
# 当复选框选中时,按钮可用;取消选中时,按钮禁用
self.PraButtonStyle.Enabled = self.CheckBox2.Checked
def CheckBox1Click(self, Sender):
"""
轮廓样式复选框的点击事件处理函数
控制按钮是否显示轮廓样式(outline风格)
参数:
Sender: 触发事件的控件对象(这里是CheckBox1)
"""
# 将按钮的轮廓样式属性设置为复选框的选中状态
# 当复选框选中时,按钮显示为轮廓样式(只有边框,没有背景色)
# 取消选中时,按钮显示为普通样式(有背景色)
self.PraButtonStyle.StyleOutline = self.CheckBox1.Checked
功能特点: - 动态控制按钮的8种主题样式(Primary、Secondary、Success、Danger、Warning、Info、Light、Dark) - 支持轮廓样式(Outline)切换 - 支持按钮启用/禁用状态控制 - 支持按钮形状切换(圆角矩形/普通矩形) - 实时预览样式变化效果
演示4: demo4 - 简单按钮展示¶
文件: demo4/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
import os
from glcl import *
class Form1(Form):
def __init__(self, owner):
self.PraButtonStyle1 = PraButtonStyle(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
功能特点: - 最简单的PraButtonStyle演示 - 展示单个按钮的基本外观 - 演示按钮的默认样式和布局
演示5: demo5 - 复杂按钮布局¶
文件: demo5/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
import os
from glcl import *
class Form1(Form):
def __init__(self, owner):
self.PraButtonStyle1 = PraButtonStyle(self)
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
功能特点: - 展示多个PraButtonStyle按钮的复杂布局 - 演示按钮在不同容器中的排列方式 - 展示按钮的各种尺寸和间距配置
演示6: demo6 - 图片轮播按钮¶
文件: demo6/main.py
# Powered By Python Studio, The best Python GUI IDE to download from glsite.com.
# 导入操作系统模块,用于文件和路径操作
import os
# 从glcl库导入所有组件(这是一个GUI框架)
from glcl import *
# 导入时间模块,用于添加延时效果
import time
class Form13(Form):
"""
主窗体类,继承自Form基类
实现了一个图片轮播预览的演示界面
通过按钮点击来循环显示ImageList中的不同图片
"""
def __init__(self, owner):
"""
构造函数,初始化窗体及其所有控件
参数:
owner: 窗体的所有者(父窗体)
"""
# 图像列表控件,用于存储多张图片资源
# 这些图片已经被预先加载到ImageList中
self.ImageList1 = ImageList(self)
# 普通按钮控件,用于触发图片轮播
self.Button1 = Button(self)
# 自定义按钮样式控件,用于显示当前选中的图片
# 这个按钮将作为图片显示区域,通过设置ImageIndexPicture属性来改变显示的图片
self.PraButtonStyle1 = PraButtonStyle(self)
# 加载窗体的布局文件(.pydfm格式)
# 使用__file__获取当前脚本的路径,构建完整的布局文件路径
self.LoadProps(os.path.join(os.path.dirname(os.path.abspath(__file__)), "main.pydfm"))
# 绑定按钮点击事件处理函数
self.Button1.OnClick = self.Button1Click
def Button1Click(self, Sender):
"""
按钮点击事件处理函数
当点击按钮时,循环遍历ImageList中的所有图片
并在自定义按钮上依次显示每张图片,实现轮播预览效果
参数:
Sender: 触发事件的控件对象(这里是Button1)
"""
# 遍历ImageList中的所有图片(从索引0到Count-1)
for i in range(0, self.ImageList1.Count):
# 设置自定义按钮的图片索引为当前循环的索引值
# ImageIndexPicture属性决定了按钮上显示ImageList中的哪张图片
self.PraButtonStyle1.ImageIndexPicture = i
# 处理所有挂起的Windows消息,确保界面能够及时更新
# 这对于循环中的界面更新非常重要,否则界面可能会卡住直到循环结束
Application.ProcessMessages()
# 暂停0.1秒,制造轮播的间隔效果
# 这样用户能够看清每张图片,而不是一闪而过
time.sleep(0.1)
功能特点: - 演示PraButtonStyle作为图片显示容器的功能 - 使用ImageList存储多张图片资源 - 实现图片轮播预览效果 - 展示按钮与图片资源的结合使用
设计文件: main.sct (示例)¶
def PraButtonStyle7Click(Sender):
def PraButtonStyle8Click(Sender):
def CheckBox1Click(Sender):
def CheckBox2Click(Sender):
def ComboBox1Select(Sender):
def ComboBox2Select(Sender):
def Button1Click(Sender):
设计文件: main.sfm (demo2示例结构)¶
object Form1: TForm
Left = 0
Top = 0
Caption = 'Form1'
ClientHeight = 600
ClientWidth = 800
object svMenu: TSplitView
Left = 0
Top = 0
Width = 204
Height = 600
Opened = True
object Panel4: TPanel
Left = 0
Top = 0
Width = 204
Height = 60
object Image1: TImage
Left = 10
Top = 10
Width = 40
Height = 40
end
object Label1: TLabel
Left = 60
Top = 20
Width = 120
Height = 20
Caption = '导航菜单'
Visible = True
end
end
object PraButtonStyle1: TPraButtonStyle
Left = 10
Top = 80
Width = 184
Height = 40
Caption = '首页'
ShowCaption = True
end
object PraButtonStyle2: TPraButtonStyle
Left = 10
Top = 130
Width = 184
Height = 40
Caption = '仪表板'
ShowCaption = True
end
object PraButtonStyle3: TPraButtonStyle
Left = 10
Top = 180
Width = 184
Height = 40
Caption = '消息'
ShowCaption = True
end
object PraButtonStyle4: TPraButtonStyle
Left = 10
Top = 230
Width = 184
Height = 40
Caption = '设置'
ShowCaption = True
end
object PraButtonStyle5: TPraButtonStyle
Left = 10
Top = 280
Width = 184
Height = 40
Caption = '帮助'
ShowCaption = True
end
object PraButtonStyle6: TPraButtonStyle
Left = 10
Top = 330
Width = 184
Height = 40
Caption = '关于'
ShowCaption = True
end
end
object Panel1: TPanel
Left = 204
Top = 0
Width = 596
Height = 600
object PraButtonStyle7: TPraButtonStyle
Left = 20
Top = 20
Width = 40
Height = 40
Caption = '≡'
OnClick = PraButtonStyle7Click
end
object PraButtonStyle8: TPraButtonStyle
Left = 520
Top = 20
Width = 40
Height = 40
Caption = '×'
OnClick = PraButtonStyle8Click
end
end
end
其他文件¶
- Extractor_Icon.ico: 应用程序图标文件
- Project1.xml: 项目配置文件
- main.pydfm: 窗体设计文件(二进制格式)
- img/: 图标资源目录(仅demo1)
详细分析¶
功能概述¶
PraButtonStyleDemo全面展示了自定义按钮样式组件的各种应用场景:
- 基本图标按钮 (demo1): 展示按钮的基本外观和图标支持
- 侧边导航菜单 (demo2): 演示按钮在导航菜单中的应用
- 按钮样式控制 (demo3): 展示按钮样式的动态控制
- 简单按钮展示 (demo4): 最简单的按钮演示
- 复杂按钮布局 (demo5): 展示多个按钮的复杂布局
- 图片轮播按钮 (demo6): 演示按钮作为图片显示容器的功能
核心功能特性¶
- 丰富的样式支持: 支持8种主题样式(Primary、Secondary、Success、Danger、Warning、Info、Light、Dark)
- 轮廓样式: 支持轮廓样式(Outline)显示,只有边框没有背景色
- 形状控制: 支持圆角矩形和普通矩形两种形状
- 图标支持: 支持在按钮上显示图标,图标可以来自ImageList或外部文件
- 动态控制: 所有样式属性都可以在运行时动态修改
- 状态控制: 支持启用/禁用状态,禁用时自动变灰
- 布局灵活: 支持各种尺寸和排列方式
使用的组件¶
1. 主窗体组件¶
uMain,Form1,Form13(TForm): 主窗体容器
2. PraButtonStyle组件¶
- 所有演示中都使用了
PraButtonStyle组件,这是演示的核心
3. 辅助组件¶
Panel(TPanel): 面板容器,用于布局SplitView(TSplitView): 分割视图,用于可折叠菜单CheckBox(TCheckBox): 复选框,用于控制按钮属性ComboBox(TComboBox): 下拉框,用于选择样式Label(TLabel): 标签,用于显示文本Image(TImage): 图像显示控件ImageList(TImageList): 图像列表,存储多个图片资源Button(TButton): 普通按钮,用于触发操作Shape(TShape): 形状控件,用于装饰
技术特点¶
1. 样式常量¶
bsPrimary: 主样式(蓝色)bsSecondary: 次级样式(灰色)bsSuccess: 成功样式(绿色)bsDanger: 危险样式(红色)bsWarning: 警告样式(黄色)bsInfo: 信息样式(浅蓝色)bsLight: 亮色样式(浅灰色)bsDark: 暗色样式(深灰色)
2. 形状常量¶
stRoundRect: 圆角矩形stRectangle: 普通矩形
3. 关键属性¶
Style: 按钮主题样式StyleOutline: 是否显示轮廓样式Shape: 按钮形状Enabled: 启用/禁用状态ShowCaption: 是否显示标题ImageIndexPicture: 图片索引(用于显示ImageList中的图片)Caption: 按钮标题文字
4. 事件处理¶
OnClick: 点击事件,当按钮被点击时触发
窗体属性¶
- caption: 根据demo不同而变化(Form1、uMain、Form13等)
- height: 400-600像素
- width: 600-800像素
代码分析¶
导入的模块:
- import os: 操作系统接口模块
- from glcl import *: GUI组件库
- import time: 时间模块(仅demo6)
定义的类:
- uMain: demo1的主窗体类
- Form1: demo2、demo3、demo4、demo5的主窗体类
- Form13: demo6的主窗体类
定义的方法:
- __init__: 构造函数,初始化窗体和控件
- UpdateShowCaption: 更新按钮标题显示状态(demo2)
- PraButtonStyle7Click: 菜单按钮点击事件(demo2)
- PraButtonStyle8Click: 关闭按钮点击事件(demo2)
- CheckBox1Click: 轮廓样式复选框点击事件(demo3)
- CheckBox2Click: 启用状态复选框点击事件(demo3)
- ComboBox1Select: 主题样式选择事件(demo3)
- ComboBox2Select: 形状选择事件(demo3)
- Button1Click: 图片轮播按钮点击事件(demo6)
事件绑定:
- self.PraButtonStyle7.OnClick = self.PraButtonStyle7Click
- self.PraButtonStyle8.OnClick = self.PraButtonStyle8Click
- self.CheckBox1.OnClick = self.CheckBox1Click
- self.CheckBox2.OnClick = self.CheckBox2Click
- self.ComboBox1.OnSelect = self.ComboBox1Select
- self.ComboBox2.OnSelect = self.ComboBox2Select
- self.Button1.OnClick = self.Button1Click
使用说明¶
1. 基本使用¶
# 创建PraButtonStyle实例
button = PraButtonStyle(self)
# 设置基本属性
button.Caption = "点击我"
button.Style = bsPrimary
button.Shape = stRoundRect
button.Enabled = True
# 绑定点击事件
button.OnClick = self.button_click
2. 动态样式切换¶
# 切换主题样式
button.Style = bsSuccess # 切换为成功样式(绿色)
# 切换轮廓样式
button.StyleOutline = True # 显示轮廓样式
# 切换形状
button.Shape = stRectangle # 切换为普通矩形
3. 与ImageList结合使用¶
# 创建ImageList并添加图片
image_list = ImageList(self)
image_list.AddFromFile("icon1.png")
image_list.AddFromFile("icon2.png")
# 设置按钮显示ImageList中的图片
button.ImageIndexPicture = 0 # 显示第一张图片
4. 在导航菜单中的应用¶
# 创建可折叠的侧边菜单
split_view = SplitView(self)
split_view.Opened = True # 默认展开
# 在SplitView中添加导航按钮
nav_button = PraButtonStyle(split_view)
nav_button.Caption = "首页"
nav_button.ShowCaption = True
应用场景¶
- 现代UI界面: 创建具有现代风格的按钮
- 导航菜单: 实现可折叠的侧边导航
- 仪表板: 创建功能丰富的控制面板
- 设置界面: 实现样式可配置的按钮
- 图片展示: 将按钮作为图片显示容器
- 社交媒体应用: 使用社交媒体图标按钮
注意事项¶
- 图标资源: 确保图标文件存在且路径正确
- ImageList索引: 设置ImageIndexPicture时要确保索引有效
- 样式兼容性: 某些样式组合可能不兼容,需要测试
- 性能考虑: 大量动态样式切换可能影响性能
- 布局适配: 在不同分辨率下测试布局效果
总结¶
PraButtonStyleDemo全面展示了PraButtonStyle组件的强大功能和灵活性。通过6个不同的演示示例,开发者可以学习到:
- 如何创建基本图标按钮
- 如何实现可折叠的导航菜单
- 如何动态控制按钮样式
- 如何将按钮用作图片显示容器
- 如何实现复杂的按钮布局
该组件特别适合需要现代UI风格的应用程序,提供了丰富的样式选项和灵活的控制方式,能够大大提升应用程序的用户体验。