-
-
Notifications
You must be signed in to change notification settings - Fork 2k
/
ParsingJson.py
152 lines (131 loc) · 4.14 KB
/
ParsingJson.py
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Created on 2018年4月8日
@author: Irony
@site: https://pyqt.site , https://github.com/PyQt5
@email: [email protected]
@file: ParsingJson
@description:
"""
import json
import webbrowser
import chardet
try:
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QWidget, \
QLabel, QSpacerItem, QSizePolicy, QHBoxLayout
except ImportError:
from PySide2.QtCore import Qt
from PySide2.QtGui import QIcon
from PySide2.QtWidgets import QApplication, QTreeWidget, QTreeWidgetItem, QWidget, \
PySide2, QSpacerItem, QSizePolicy, QHBoxLayout
class ItemWidget(QWidget):
"""自定义的item"""
def __init__(self, text, badge, *args, **kwargs):
super(ItemWidget, self).__init__(*args, **kwargs)
layout = QHBoxLayout(self)
layout.setContentsMargins(0, 0, 0, 0)
layout.addWidget(QLabel(text, self, styleSheet='color: white;'))
layout.addSpacerItem(QSpacerItem(
60, 1, QSizePolicy.Maximum, QSizePolicy.Minimum))
if badge and len(badge) == 2: # 后面带颜色的标签
layout.addWidget(QLabel(
badge[0], self, alignment=Qt.AlignCenter,
styleSheet="""min-width: 80px;
max-width: 80px;
min-height: 38px;
max-height: 38px;
color: white;
border:none;
border-radius: 4px;
background: %s""" % badge[1]
))
class JsonTreeWidget(QTreeWidget):
def __init__(self, *args, **kwargs):
super(JsonTreeWidget, self).__init__(*args, **kwargs)
self.setEditTriggers(self.NoEditTriggers)
self.header().setVisible(False)
# 帮点单击事件
self.itemClicked.connect(self.onItemClicked)
def onItemClicked(self, item):
"""item单击事件"""
if item.url: # 调用浏览器打开网址
webbrowser.open_new_tab(item.url)
def parseData(self, datas, parent=None):
"""解析json数据"""
for data in datas:
url = data.get('url', '')
items = data.get('items', [])
# 生成item
_item = QTreeWidgetItem(parent)
_item.setIcon(0, QIcon(data.get('icon', '')))
_widget = ItemWidget(
data.get('name', ''),
data.get('badge', []),
self
)
_item.url = url # 可以直接设置变量值
self.setItemWidget(_item, 0, _widget)
if url:
continue # 跳过
# 解析儿子
if items:
self.parseData(items, _item)
def loadData(self, path):
"""加载json数据"""
datas = open(path, 'rb').read()
datas = datas.decode(chardet.detect(datas).get('encoding', 'utf-8'))
self.parseData(json.loads(datas), self)
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
app.setStyleSheet("""QTreeView {
outline: 0px;
background: rgb(47, 64, 78);
}
QTreeView::item {
min-height: 92px;
}
QTreeView::item:hover {
background: rgb(41, 56, 71);
}
QTreeView::item:selected {
background: rgb(41, 56, 71);
}
QTreeView::item:selected:active{
background: rgb(41, 56, 71);
}
QTreeView::item:selected:!active{
background: rgb(41, 56, 71);
}
QTreeView::branch:open:has-children {
background: rgb(41, 56, 71);
}
QTreeView::branch:has-siblings:!adjoins-item {
background: green;
}
QTreeView::branch:closed:has-children:has-siblings {
background: rgb(47, 64, 78);
}
QTreeView::branch:has-children:!has-siblings:closed {
background: rgb(47, 64, 78);
}
QTreeView::branch:open:has-children:has-siblings {
background: rgb(41, 56, 71);
}
QTreeView::branch:open:has-children:!has-siblings {
background: rgb(41, 56, 71);
}
QTreeView:branch:hover {
background: rgb(41, 56, 71);
}
QTreeView:branch:selected {
background: rgb(41, 56, 71);
}
""")
w = JsonTreeWidget()
w.show()
w.loadData('Data/data.json')
sys.exit(app.exec_())