使用PyQt5写python代码读取字典中的key以及key里面嵌套的key,将其设置为子节点和下一子节点(有value但不显示在节点里),每个节点都形成复选框可以自由选择并生成图形化界面
时间: 2024-05-03 18:20:14 浏览: 148
python万年历有农历节气pyqt5制作UI界面,毕业系统设计
5星 · 资源好评率100%
您好,关于您的问题,我可以提供以下示例代码供您参考:
```
import PyQt5.QtCore
import PyQt5.QtWidgets
class TreeNode:
def __init__(self, name, value=None, children=None):
self.name = name
self.value = value
self.children = children or []
def addChild(self, child):
self.children.append(child)
class TreeModel(PyQt5.QtCore.QAbstractItemModel):
def __init__(self, root, parent=None):
super(TreeModel, self).__init__(parent)
self.rootNode = root
def columnCount(self, parent=PyQt5.QtCore.QModelIndex()):
return 2
def data(self, index, role):
if role == PyQt5.QtCore.Qt.DisplayRole:
if index.column() == 0:
node = index.internalPointer()
return node.name
if index.column() == 1:
node = index.internalPointer()
if node.value:
return node.value
if role == PyQt5.QtCore.Qt.CheckStateRole and index.column() == 0:
node = index.internalPointer()
return node.checkState
return None
def flags(self, index):
return PyQt5.QtCore.Qt.ItemIsSelectable | PyQt5.QtCore.Qt.ItemIsEnabled | PyQt5.QtCore.Qt.ItemIsUserCheckable
def headerData(self, section, orientation, role):
if role == PyQt5.QtCore.Qt.DisplayRole and orientation == PyQt5.QtCore.Qt.Horizontal:
if section == 0:
return "Node Name"
if section == 1:
return "Value"
return None
def index(self, row, column, parent=PyQt5.QtCore.QModelIndex()):
if not self.hasIndex(row, column, parent):
return PyQt5.QtCore.QModelIndex()
if not parent.isValid():
parentNode = self.rootNode
else:
parentNode = parent.internalPointer()
childNode = parentNode.children[row]
return self.createIndex(row, column, childNode)
def parent(self, index):
if not index.isValid():
return PyQt5.QtCore.QModelIndex()
childNode = index.internalPointer()
parentNode = childNode.parent
if parentNode == self.rootNode:
return PyQt5.QtCore.QModelIndex()
return self.createIndex(parentNode.row, 0, parentNode)
def rowCount(self, parent=PyQt5.QtCore.QModelIndex()):
if not parent.isValid():
parentNode = self.rootNode
else:
parentNode = parent.internalPointer()
return len(parentNode.children)
def setData(self, index, value, role=PyQt5.QtCore.Qt.EditRole):
if role == PyQt5.QtCore.Qt.CheckStateRole and index.column() == 0:
node = index.internalPointer()
node.checkState = value
return True
return False
class TreeWidget(PyQt5.QtWidgets.QWidget):
def __init__(self, dictData, parent=None):
super(TreeWidget, self).__init__(parent)
self.layout = PyQt5.QtWidgets.QVBoxLayout(self)
self.treeView = PyQt5.QtWidgets.QTreeView()
self.layout.addWidget(self.treeView)
rootNode = self.buildTree(dictData)
self.treeModel = TreeModel(rootNode)
self.treeView.setModel(self.treeModel)
self.treeView.expandAll()
def buildTree(self, dictData):
rootNode = TreeNode("Root")
for key, value in dictData.items():
node = TreeNode(key, value)
if isinstance(value, dict):
for subKey in value:
subNode = TreeNode(subKey, value[subKey])
node.addChild(subNode)
rootNode.addChild(node)
return rootNode
if __name__ == '__main__':
dictData = {
"fruit": {
"apple": 1,
"orange": 2
},
"vehicle": {
"car": {
"make": "Toyota",
"model": "Corolla"
},
"truck": {
"make": "Ford",
"model": "F-150"
}
}
}
app = PyQt5.QtWidgets.QApplication([])
widget = TreeWidget(dictData)
widget.show()
app.exec_()
```
这个示例代码演示了如何使用 PyQt5 构建一个树形控件,树形结构基于一个字典的数据,复选框可以选择节点,生成图形化界面。代码实现中,我们通过自定义一个 TreeNode 类来表示树形结构中的每个节点,然后使用一个 TreeModel 类进行数据模型的构建;同时,我们定义了一个 TreeWidget 类,使用 QTreeView 控件来展示树形结构,并且将 TreeModel 和 TreeView 绑定在一起。
在调用 TreeWidget 构造函数时,我们将要展示的字典数据作为输入参数传递给它。根据字典数据的嵌套情况,我们可以使用递归的方式来遍历它,并且将键值对转换为 TreeNode 对象,接着通过 addChild 方法建立树形结构。如果一个键的值是一个字典类型,那么我们还需要递归地将它转换为子节点,并且递归方式的进一步构建树形结构。
最后,我们将完整的树形数据结构传递给 TreeModel,使用 QTreeView 控件展示出来。QTreeView 支持多选择,我们还实现了复选框功能,可以根据需求自由勾选或取消节点,并且在生成的图形化界面上展示选择结果。
阅读全文