forked from PyQt5/PyQt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
LeftTabStacked.py
110 lines (98 loc) · 3.45 KB
/
LeftTabStacked.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from random import randint
from PyQt5.QtCore import Qt, QSize
from PyQt5.QtGui import QIcon
from PyQt5.QtWidgets import QWidget, QListWidget, QStackedWidget, QHBoxLayout,\
QListWidgetItem, QLabel
# Created on 2018年5月29日
# author: Irony
# site: https://pyqt5.com , https://github.com/892768447
# email: 892768447@qq.com
# file: LeftTabWidget
# description:
__Author__ = """By: Irony
QQ: 892768447
Email: 892768447@qq.com"""
__Copyright__ = 'Copyright (c) 2018 Irony'
__Version__ = 1.0
class LeftTabWidget(QWidget):
def __init__(self, *args, **kwargs):
super(LeftTabWidget, self).__init__(*args, **kwargs)
self.resize(800, 600)
#左右布局(左边一个QListWidget + 右边QStackedWidget)
layout = QHBoxLayout(self, spacing=0)
layout.setContentsMargins(0, 0, 0, 0)
# 左侧列表
self.listWidget = QListWidget(self)
layout.addWidget(self.listWidget)
# 右侧层叠窗口
self.stackedWidget = QStackedWidget(self)
layout.addWidget(self.stackedWidget)
self.initUi()
def initUi(self):
# 初始化界面
# 通过QListWidget的当前item变化来切换QStackedWidget中的序号
self.listWidget.currentRowChanged.connect(
self.stackedWidget.setCurrentIndex)
# 去掉边框
self.listWidget.setFrameShape(QListWidget.NoFrame)
# 隐藏滚动条
self.listWidget.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
self.listWidget.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
# 这里就用一般的文本配合图标模式了(也可以直接用Icon模式,setViewMode)
for i in range(20):
item = QListWidgetItem(
QIcon('Data/0%d.ico' % randint(1, 8)), str('选 项 %s' % i), self.listWidget)
# 设置item的默认宽高(这里只有高度比较有用)
item.setSizeHint(QSize(16777215, 60))
# 文字居中
item.setTextAlignment(Qt.AlignCenter)
# 再模拟20个右侧的页面(就不和上面一起循环放了)
for i in range(20):
label = QLabel('我是页面 %d' % i, self)
label.setAlignment(Qt.AlignCenter)
# 设置label的背景颜色(这里随机)
# 这里加了一个margin边距(方便区分QStackedWidget和QLabel的颜色)
label.setStyleSheet('background: rgb(%d, %d, %d);margin: 50px;' % (
randint(0, 255), randint(0, 255), randint(0, 255)))
self.stackedWidget.addWidget(label)
# 美化样式表
Stylesheet = """
/*去掉item虚线边框*/
QListWidget, QListView, QTreeWidget, QTreeView {
outline: 0px;
}
/*设置左侧选项的最小最大宽度,文字颜色和背景颜色*/
QListWidget {
min-width: 120px;
max-width: 120px;
color: white;
background: black;
}
/*被选中时的背景颜色和左边框颜色*/
QListWidget::item:selected {
background: rgb(52, 52, 52);
border-left: 2px solid rgb(9, 187, 7);
}
/*鼠标悬停颜色*/
HistoryPanel::item:hover {
background: rgb(52, 52, 52);
}
/*右侧的层叠窗口的背景颜色*/
QStackedWidget {
background: rgb(30, 30, 30);
}
/*模拟的页面*/
QLabel {
color: white;
}
"""
if __name__ == '__main__':
import sys
from PyQt5.QtWidgets import QApplication
app = QApplication(sys.argv)
app.setStyleSheet(Stylesheet)
w = LeftTabWidget()
w.show()
sys.exit(app.exec_())