Py文件
# file: sidebar.py
import sys
from PySide2.QtCore import QUrl
from PySide2.QtCore import QSize, Qt
from PySide2.QtWidgets import QWidget
from PySide2.QtWidgets import QHBoxLayout
from PySide2.QtWidgets import QApplication
from PySide2.QtWidgets import QListWidgetItem
from PySide2.QtWebEngineWidgets import QWebEngineView
from PySide2.QtWidgets import QListWidget,QStackedWidget
class LeftTabWidget(QWidget):
'''左侧选项栏'''
def __init__(self):
super(LeftTabWidget, self).__init__()
self.setObjectName('LeftTabWidget')
self.setWindowTitle('LeftTabWidget')
with open('QListWidgetQSS.qss', 'r') as f: #导入QListWidget的qss样式
self.list_style = f.read()
self.main_layout = QHBoxLayout(self, spacing=0) #窗口的整体布局
self.main_layout.setContentsMargins(0,0,0,0)
self.left_widget = QListWidget() #左侧选项列表
self.left_widget.setStyleSheet(self.list_style)
self.main_layout.addWidget(self.left_widget)
self.right_widget = QStackedWidget()
self.main_layout.addWidget(self.right_widget)
self._setup_ui()
def _setup_ui(self):
'''加载界面ui'''
self.left_widget.currentRowChanged.connect(self.right_widget.setCurrentIndex) #list和右侧窗口的index对应绑定
self.left_widget.setFrameShape(QListWidget.NoFrame) #去掉边框
self.left_widget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff) #隐藏滚动条
self.left_widget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
list_str = ['岗位需求','专业要求','薪水分布','城市分布']
# 具体html文件自己制作
url_list = ['job_num_wordcloud.html', 'edu_need.html', 'salary_bar.html', 'edu_salary_bar.html']
for i in range(4):
self.item = QListWidgetItem(list_str[i],self.left_widget) #左侧选项的添加
self.item.setSizeHint(QSize(30,60))
self.item.setTextAlignment(Qt.AlignCenter) #居中显示
self.browser = QWebEngineView() #右侧用QWebView来显示html网页
# 具体地址自己修改
self.browser.setUrl(QUrl.fromLocalFile('C://Users//Administrator//Desktop//%s' % url_list[i]))
self.right_widget.addWidget(self.browser)
def main():
''' '''
app = QApplication(sys.argv)
main_wnd = LeftTabWidget()
main_wnd.show()
app.exec_()
if __name__ == '__main__':
main()
qss 文件
QListWidget, QListView, QTreeWidget, QTreeView {
outline: 0px;
}
QListWidget {
min-width: 120px;
max-width: 120px;
color: Black;
background: #F5F5F5;
}
QListWidget::Item:selected {
background: lightGray;
border-left: 5px solid red;
}
HistoryPanel:hover {
background: rgb(52, 52, 52);
}
主要参考