规则:
- 列表第一项描述
QHBox
orQVBox
- 第二项描述是否可拉伸,也就是
QBox
orQSplitter
- 列表可嵌套
渲染:
def fun(self, ui):
if ui[0] == "H":
if not ui[1]:
box = QHBoxLayout()
else:
box = QSplitter(Qt.Horizontal)
elif ui[0] == "V":
if not ui[1]:
box = QVBoxLayout()
else:
box = QSplitter(Qt.Vorizontal)
for i in ui[2:]:
if isinstance(i, list):
box.addLayout(self.fun(i))
else:
# print(i)
box.addWidget(i)
return box
举个例子:
假如我们想创建这样的窗口那么我们的数据结构是这样的
['V', False,
QPushButton("按钮1", self),
QPushButton("按钮2", self),
["H", False, QPushButton("按钮3", self),QPushButton("按钮4", self)]
]
完整代码:
#!/usr/bin/python3
import os
import sys
from PyQt5 import QtCore
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
class Example(QWidget):
def __init__(self):
super().__init__()
self.ui = ['V', False,
QPushButton("按钮1", self),
QPushButton("按钮2", self),
["H", False, QPushButton("按钮3", self),
QPushButton("按钮4", self)]
]
self.initUI()
def fun(self, ui):
if ui[0] == "H":
if not ui[1]:
box = QHBoxLayout()
else:
box = QSplitter(Qt.Horizontal)
elif ui[0] == "V":
if not ui[1]:
box = QVBoxLayout()
else:
box = QSplitter(Qt.Vorizontal)
for i in ui[2:]:
if isinstance(i, list):
box.addLayout(self.fun(i))
else:
# print(i)
box.addWidget(i)
return box
def initUI(self):
self.setLayout(self.fun(self.ui))
self.show()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
标签:box,__,Qt,self,弹性,json,ui,QPushButton,按钮 From: https://www.cnblogs.com/Ctrl-cCtrl-v/p/16814613.html