Pyqt5 關(guān)于流式布局和滾動(dòng)條的綜合使用示例代碼
流式布局
所謂流式布局指的是容器中的元素像流水一樣,是可以浮動(dòng)的,當(dāng)元素一行或者一列占滿的時(shí)候,它會(huì)自動(dòng)流入到下一行或者下一列。
pyqt5流式布局
pyqt中采用流式布局的方法原理是,通過(guò)contentsMargins獲取到子元素距離布局的上下左右寬度,然后我們將所有子元素進(jìn)行遍歷,如果它加上邊距可以在一行放入的話,那么就放在一行內(nèi),如果不能,就放入到下一行,具體代碼如下:
m = self.contentsMargins()
effective_rect = rect.adjusted(+m.left(), +m.top(), -m.right(), -m.bottom())
x = effective_rect.x()
y = effective_rect.y()
line_height = 0
for item in self._item_list:
wid = item.widget()
space_x = self.spacing()
space_y = self.spacing()
if wid is not None:
space_x += wid.style().layoutSpacing(
QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal)
space_y += wid.style().layoutSpacing(
QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical)
next_x = x + item.sizeHint().width() + space_x
if next_x - space_x > effective_rect.right() and line_height > 0:
x = effective_rect.x()
y = y + line_height + space_y
next_x = x + item.sizeHint().width() + space_x
line_height = 0
if not test_only:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = next_x
line_height = max(line_height, item.sizeHint().height())
滾動(dòng)條的設(shè)置
pyqt中有專門的滾動(dòng)條組件QScrollBar,這個(gè)組件需要配合其他組件使用,我們這里使用QScrollArea這個(gè)組件進(jìn)行滾動(dòng)條的設(shè)置。
滾動(dòng)條的使用方法
首先,我們需要聲明QScrollArea
然后,我們需要設(shè)置QScrollArea的位置大小
最后,我們將需要產(chǎn)生滾動(dòng)條的元素放入它的內(nèi)部。
q = QWidget() qscrollarea = QtWidgets.QScrollArea(q) qscrollarea.setGeometry(QRect(50,100,600,500)) qscrollarea.setWidgetResizable(True) listWidget = QtWidgets.QListWidget() qscrollarea.setWidget(listWidget)
流式布局和滾動(dòng)條的結(jié)合案例:
在文件當(dāng)前目錄創(chuàng)建一個(gè)images文件夾,然后放入想要展示的多張圖片,然后執(zhí)行當(dāng)前程序,就會(huì)看到帶有滾動(dòng)條的流式布局界面。
運(yùn)行程序,需要安裝pyqt5

from PyQt5.QtCore import QPoint, QRect, QSize, Qt,pyqtSignal
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (
QApplication, QLayout, QPushButton, QSizePolicy, QWidget, QGridLayout)
import sys
class Window(QWidget):
def __init__(self):
self.imageheight = 100
super(Window, self).__init__()
self.resize(800, 600)
#self.listwidget = QtWidgets.QListWidget(self)
#self.listwidget.resize(400,300)
#self.listwidget.setGeometry(QtCore.QRect(0, 0, 300, 200))
#self.listwidget.addItem("test")
highlight_dir = r"./images"
self.files_it = iter([os.path.join(highlight_dir, file)
for file in os.listdir(highlight_dir)])
# self.centralwidget = QtWidgets.QWidget(MainWindow)
# self.gongzuomoshi = QtWidgets.QGroupBox(self.centralwidget)
self.listWidget = QtWidgets.QListWidget(self)
#self.listWidget.setFixedWidth(600)
container_layout = QtWidgets.QVBoxLayout()
g = QtWidgets.QGroupBox('')
l = FlowLayout()
g.setLayout(l)
for file in iter(self.files_it):
pixmap = QtGui.QPixmap(file)
if not pixmap.isNull():
autoWidth = pixmap.width()*self.imageheight/pixmap.height()
label = QtWidgets.QLabel(pixmap=pixmap)
label.setScaledContents(True)
label.setFixedHeight(self.imageheight)
label.setFixedWidth(autoWidth)
l.addWidget(label)
container_layout.addWidget(g)
container_layout.addStretch()
self.listWidget.setLayout(container_layout)
self.qscrollarea = QtWidgets.QScrollArea(self)
self.qscrollarea.setGeometry(QRect(50,100,600,500))
self.qscrollarea.setWidgetResizable(True)
self.qscrollarea.setWidget(self.listWidget)
self.setWindowTitle("Flow Layout Scroll")
class FlowLayout(QLayout):
"""流式布局,使用說(shuō)明
1.聲明流式布局 layout = FlowLayout
2.將元素放入流式布局中
3.將QGroupBox應(yīng)用流式布局
4.如果期望水平流式,將QGroupBox放入到QHBoxLayout,如果期望垂直布局,將QGroupBox放入到QVBoxLayout
"""
heightChanged = pyqtSignal(int)
def __init__(self, parent=None, margin=0, spacing=-1):
super().__init__(parent)
if parent is not None:
self.setContentsMargins(margin, margin, margin, margin)
self.setSpacing(spacing)
self._item_list = []
def __del__(self):
while self.count():
self.takeAt(0)
def addItem(self, item): # pylint: disable=invalid-name
self._item_list.append(item)
def addSpacing(self, size): # pylint: disable=invalid-name
self.addItem(QSpacerItem(size, 0, QSizePolicy.Fixed, QSizePolicy.Minimum))
def count(self):
return len(self._item_list)
def itemAt(self, index): # pylint: disable=invalid-name
if 0 <= index < len(self._item_list):
return self._item_list[index]
return None
def takeAt(self, index): # pylint: disable=invalid-name
if 0 <= index < len(self._item_list):
return self._item_list.pop(index)
return None
def expandingDirections(self): # pylint: disable=invalid-name,no-self-use
return Qt.Orientations(Qt.Orientation(0))
def hasHeightForWidth(self): # pylint: disable=invalid-name,no-self-use
return True
def heightForWidth(self, width): # pylint: disable=invalid-name
height = self._do_layout(QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect): # pylint: disable=invalid-name
super().setGeometry(rect)
self._do_layout(rect, False)
def sizeHint(self): # pylint: disable=invalid-name
return self.minimumSize()
def minimumSize(self): # pylint: disable=invalid-name
size = QSize()
for item in self._item_list:
minsize = item.minimumSize()
extent = item.geometry().bottomRight()
size = size.expandedTo(QSize(minsize.width(), extent.y()))
margin = self.contentsMargins().left()
size += QSize(2 * margin, 2 * margin)
return size
def _do_layout(self, rect, test_only=False):
m = self.contentsMargins()
effective_rect = rect.adjusted(+m.left(), +m.top(), -m.right(), -m.bottom())
x = effective_rect.x()
y = effective_rect.y()
line_height = 0
for item in self._item_list:
wid = item.widget()
space_x = self.spacing()
space_y = self.spacing()
if wid is not None:
space_x += wid.style().layoutSpacing(
QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Horizontal)
space_y += wid.style().layoutSpacing(
QSizePolicy.PushButton, QSizePolicy.PushButton, Qt.Vertical)
next_x = x + item.sizeHint().width() + space_x
if next_x - space_x > effective_rect.right() and line_height > 0:
x = effective_rect.x()
y = y + line_height + space_y
next_x = x + item.sizeHint().width() + space_x
line_height = 0
if not test_only:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = next_x
line_height = max(line_height, item.sizeHint().height())
new_height = y + line_height - rect.y()
self.heightChanged.emit(new_height)
return new_height
if __name__ == '__main__':
app = QApplication(sys.argv)
mainWin = Window()
mainWin.show()
sys.exit(app.exec_())
到此這篇關(guān)于Pyqt5 關(guān)于流式布局和滾動(dòng)條的綜合使用示例代碼的文章就介紹到這了,更多相關(guān)Pyqt5 流式布局和滾動(dòng)條內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python GUI學(xué)習(xí)之登錄系統(tǒng)界面篇
這篇文章主要介紹了Python GUI學(xué)習(xí)之登錄系統(tǒng)界面篇,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
Python利用memory_profiler實(shí)現(xiàn)內(nèi)存分析
memory_profiler是第三方模塊,用于監(jiān)視進(jìn)程的內(nèi)存消耗以及python程序內(nèi)存消耗的逐行分析。本文將利用memory_profiler實(shí)現(xiàn)內(nèi)存分析,需要的可以參考一下2022-10-10
Python多線程threading和multiprocessing模塊實(shí)例解析
這篇文章主要介紹了Python多線程threading和multiprocessing模塊等相關(guān)內(nèi)容,分享了相關(guān)代碼示例,小編覺得還是挺不錯(cuò)的,這里分享給大家,需要的朋友可以參考下2018-01-01
python設(shè)計(jì)并實(shí)現(xiàn)平面點(diǎn)類Point的源代碼
這篇文章主要介紹了python-設(shè)計(jì)并實(shí)現(xiàn)平面點(diǎn)類Point,定義一個(gè)平面點(diǎn)類Point,對(duì)其重載運(yùn)算符關(guān)系運(yùn)算符,關(guān)系運(yùn)算以距離坐標(biāo)原點(diǎn)的遠(yuǎn)近作為基準(zhǔn),需要的朋友可以參考下2024-05-05

