PySide2重要模板

(〇)模板

【青春版】【自力更生版】

1
2
3
4
5
6
7
8
9
10
11
12
from PySide2.QtWidgets import *
# -----------------------------------------
app = QApplication([])
# -----------------------------------------
window = QMainWindow()
window.resize(800, 600), window.move(300, 310), window.setWindowTitle('Title')
# -----------------------------------------
#
# -----------------------------------------
window.show()
app.exec_()

【至尊版】【完全形态版】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
from PySide2.QtWidgets import *
# -----------------------------------------
class Properties:
def __init__(self):
# 公共信息-----------------------------------------
self.desktop = QApplication.desktop()
self.width = self.desktop.width()
self.height = self.desktop.height()
# 主窗口-----------------------------------------
self.window = QMainWindow()
self.window.resize(500, 500)
self.window.move(int(self.width/2) - 250, int(self.height/2) - 250)
self.window.setWindowTitle('Title')
# 信号-----------------------------------------
self.button = QPushButton('button', self.window)
self.button.move(10, 20)
# 信号槽-----------------------------------------
self.button.clicked.connect(self.slot_sample)
# 槽-----------------------------------------
def slot_sample(self):
QMessageBox.about(self.window, 'about', 'hello world')
# 启动-----------------------------------------
app = QApplication([])
prop = Properties()
prop.window.show()
app.exec_()

【臻享版】【极简奢华版】

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from PySide2.QtWidgets import *
from PySide2.QtUiTools import QUiLoader
from PySide2.QtCore import QFile
from PySide2.QtGui import QIcon
import PySide2.QtXml
from threading import Thread, Lock
from time import sleep
import os.path as pa
# -----------------------------------------
res = '../Assets' # pa.join(res, '')
# -----------------------------------------
class Properties:
def __init__(self):
# -----------------------------------------
UI = QFile(pa.join(res, 'UI/mainWindow.ui'))
UI.open(QFile.ReadOnly), UI.close()
self.ui = QUiLoader().load(UI) # 界面文件
# -----------------------------------------
self.window = self.ui.window() # 主窗口
# -----------------------------------------
self.ui.button.clicked.connect(self.slot_sample)
# -----------------------------------------
def slot_sample(self):
QMessageBox.about(self.window, 'about', 'hello world')
# -----------------------------------------
app = QApplication([])
app.setWindowIcon(QIcon(pa.join(res, 'Sprites/zt.jpg')))
prop = Properties()
prop.window.show()
app.exec_()

(一)QSS样式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# 语法
selector选择器
{
properties属性:values值;
}
# --------------------------------------------
# 选择器 selector
# --------------------------------------------
# * 所有类
# QLabel 特定类(包括子类)
# .QLabel 特定类(不包括子类)
# QLabel#lab 特定类(按名称筛选)
# QLabel[enable="true"] 特定类(按属性筛选)
# QGroupBox QLabel 特定类(限制父类)
# QGroupBox * 所有类(限制父类)
# QGroupBox > QLabel 特定类(直接子节点)
# QLabel:hover 伪状态(悬停)
# QLabel:disabled 伪状态(不可用)
# QLabel:hover:checked 伪状态(悬停勾选)
# --------------------------------------------
# 属性 properties
# --------------------------------------------
# background-color:#00FF00; 背景色
# background-image:url(/); 背景图
# icon:(/); 图标
# icon-size:20px; 图标大小
# opacity:255; 透明度
# border:1px solid/dashed/dotted #00FF00; 边框
# font-family:“Arial”; 字体
# font-size:15px; 字体大小
# color: #1d649c; 字体颜色
# font: bold italic large "Arial"; 字体列表
# text-align:left; 文字对齐
# width:50px; 宽
# height:20px; 高
# margin:10px 11px 12px 13px; 外界
# margin-top/right/bottom/left:10px; 单独外界
# margin:10px 11px; 外界上下和左右
# padding:10px 11px 12px 13px; 内界
# padding-top/right/bottom/left:10px; 单独内界
# padding:10px 11px; 内界上下和左右