Python GUI——Guietta
Guietta 是一个Python GUI 框架,使用了
PySide2
,简单理解就是python使用的Qt,具体见 Github入手比较方便,类似于PySimpleGUI,我之前试着使用,写了一篇笔记在CSDN,但是后来没有深入的去研究,就搁浅了,最近搜索看到
Guiette
这个库不错,就想着学习一下。
一、入门
环境介绍:
- OS:Windows 10 64bit
- Python Version :3.8.X
- Guietta Version :0.6.0
- 编辑器 :Visual Studio Code
1、安装 Guietta
# 安装 Guietta
pip install guietta
# 如果慢可以使用阿里的源
# pip install guietta -i https://mirrors.aliyun.com/pypi/simple
2、快速开始
这里我们就跑一下官方文档的代码吧
from guietta import Gui, _
gui = Gui( [ 'Enter number' , '__num__' , ['Go'] ],
[ 'Result ---> ' , 'result' , _ ] )
gui.run()
结果如下:
从代码可以清晰的看到软件实际的布局:
很棒对吧!
接下来我们根据文档来解释一下这些晦涩⛄的代码:
布局主要根据传递给Gui()
的列表来定义,一个列表就会渲染为一行,示例代码中有两个列表,所以软件布局是两行。
'Enter number'
:这个其实是 Label,等效于 L(‘Enter number’),这里是简化写法。'__num__'
:这个是个编辑框,num 是他的名字(暂且这么理解)。['Go']
:这是个按钮,等效于B(‘Go’),这里是简化写法。_
:一个下划线,可以理解为一个占位符,就是空的。
是不是发现点击了 Go
按钮,什么也没有发生,因为这里的 Go
按钮啥功能也没有绑定。gui.run()
以后,就进入了 QT的事件循环,直到关闭程序结束。当然作者还提供了其他的方式来运行GUI,但是很不巧,我写这篇笔记的时候还没有真正理解,你们自己探索吧。
3、Note
鉴于大家看博客都是为了寻找快速解决问题的方法,我这里放几个我遇到的问题和对应的解决方法。
# 如果后续想使用 Matplotlib 和 Pyqtgraph 这里建议提前安装
pip install Matplotlib Pyqtgraph
# 设置了python 虚拟环境,找不到 guietta 模块
这个可能是我的配置的问题,切换全局环境就好了
二、实例
先略
三、官方文档摘录
1、魔法属性(magic properties)
有道词典这么告诉我的,哈哈哈哈,但是怕误导大家,就把英文原名放这里了,先这么叫!
学完回来写
2、Widgets(组件)
from guietta import Gui, B, E, L, HS, VS, HSeparator, VSeparator
from guietta import Yes, No, Ok, Cancel, Quit, _, ___, III
gui = Gui(
[ '<center>A big GUI with all of Guietta''s widgets</center>'],
[ HSeparator ],
[ 'Label' , 'imagelabel.jpeg' , L('another label') , VS('slider1')],
[ _ , ['button'] , B('another button') , III ],
[ '__edit__' , E('an edit box') , _ , VSeparator ],
[ Quit , Ok , Cancel , III ],
[ Yes , No , _ , III ],
[ HS('slider2'), ___ , ___ , _ ] )
# gui.show()
gui.run()
运行结果如下:
Syntax | Equivalent Qt widget | Event name |
---|---|---|
_ | nothing (empty layout cell) | none |
‘text’L(‘text’) | QLabel(‘text’) | ‘text’ |
‘image.jpg’L(‘image.jpg’) | QLabel with QPixmap(‘image.jpg’) | ‘image’ |
[‘text’]B(‘text’) | QPushButton(‘text’) | ‘text’ |
[‘image.jpg’]B(‘image.jpg’) | QPushButton(QIcon(‘image.jpg’), ‘’) | ‘image’ |
‘name’ | QLineEdit(‘’), name set to ‘name’ | ‘name’ |
E(‘text’) | QLineEdit(‘text’) | ‘text’ |
C(‘text’) | QCheckBox(‘text’) | ‘text’ |
R(‘text’) | QRadioButton(‘text’) | ‘text’ |
P(‘name’) | QProgressBar() | ‘name’ |
PW(‘name’) | QLineEdit() passsword-style (text is replaced with circles) | ‘name’ |
G(‘title’) | QGroupBox(‘title’) | ‘title’ |
HS(‘name’) | QSlider(Qt::Horizontal) | ‘name’ |
VS(‘name’) | QSlider(Qt::Horizontal) | ‘name’ |
HSeparator | Horizontal separator | |
VSeparator | Vertical separator | |
M(‘name’) | Matplotlib FigureCanvas* | |
PG(‘name’) | pyqtgraph PlotWidget* | |
PGI(‘name’) | pyqtgraph ImageView* | |
widget | any valid QT widget | none |
(widget, ‘name’) | any valid QT widget | ‘name’ |