python GUI框架pyqt5 對(duì)圖片進(jìn)行流式布局的方法(瀑布流flowlayout)
流式布局
流式布局,也叫做瀑布流布局,是網(wǎng)頁(yè)中經(jīng)常使用的一種頁(yè)面布局方式,它的原理就是將高度固定,然后圖片的寬度自適應(yīng),這樣加載出來(lái)的圖片看起來(lái)就像瀑布一樣整齊的水流淌下來(lái)。
pyqt流式布局
那么在pyqt5中我們?cè)趺词褂昧魇讲季帜??pyqt沒(méi)有這個(gè)控件,需要我們自己去封裝,下面是流式布局的封裝代碼。
class FlowLayout(QLayout):
def __init__(self, parent=None, margin=0, spacing=-1):
super(FlowLayout, self).__init__(parent)
if parent is not None:
self.setContentsMargins(margin, margin, margin, margin)
self.setSpacing(spacing)
self.itemList = []
def __del__(self):
item = self.takeAt(0)
while item:
item = self.takeAt(0)
def addItem(self, item):
self.itemList.append(item)
def count(self):
return len(self.itemList)
def itemAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList[index]
return None
def takeAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList.pop(index)
return None
def expandingDirections(self):
return Qt.Orientations(Qt.Orientation(0))
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
height = self.doLayout(QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QSize()
for item in self.itemList:
size = size.expandedTo(item.minimumSize())
margin, _, _, _ = self.getContentsMargins()
size += QSize(2 * margin, 2 * margin)
return size
def doLayout(self, rect, testOnly):
x = rect.x()
y = rect.y()
lineHeight = 0
for item in self.itemList:
wid = item.widget()
spaceX = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton,
QSizePolicy.PushButton, Qt.Horizontal)
spaceY = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton,
QSizePolicy.PushButton, Qt.Vertical)
nextX = x + item.sizeHint().width() + spaceX
if nextX - spaceX > rect.right() and lineHeight > 0:
x = rect.x()
y = y + lineHeight + spaceY
nextX = x + item.sizeHint().width() + spaceX
lineHeight = 0
if not testOnly:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = nextX
lineHeight = max(lineHeight, item.sizeHint().height())
return y + lineHeight - rect.y()
封裝好的流式布局類,我們只要傳入相應(yīng)的layout之后,他就會(huì)自動(dòng)計(jì)算頁(yè)面的元素,適應(yīng)頁(yè)面的寬度。
下面是我們寫(xiě)的一個(gè)瀑布流顯示圖片的代碼:
from PyQt5.QtCore import QPoint, QRect, QSize, Qt
import os
from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import (
QApplication, QLayout, QPushButton, QSizePolicy, QWidget, QGridLayout)
class Window(QWidget):
def __init__(self):
self.imageheight = 100
super(Window, self).__init__()
self.resize(400, 300)
flowLayout = FlowLayout()
highlight_dir = "./"
self.files_it = iter([os.path.join(highlight_dir, file)
for file in os.listdir(highlight_dir)])
print()
for file in iter(self.files_it):
layout = QGridLayout()
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)
print(autoWidth)
label.setFixedWidth(autoWidth)
#label.setFixedSize(100, 50)
layout.addWidget(label)
widget = QWidget()
widget.setLayout(layout)
flowLayout.addWidget(widget)
self.setLayout(flowLayout)
self.setWindowTitle("Flow Layout")
class FlowLayout(QLayout):
def __init__(self, parent=None, margin=0, spacing=-1):
super(FlowLayout, self).__init__(parent)
if parent is not None:
self.setContentsMargins(margin, margin, margin, margin)
self.setSpacing(spacing)
self.itemList = []
def __del__(self):
item = self.takeAt(0)
while item:
item = self.takeAt(0)
def addItem(self, item):
self.itemList.append(item)
def count(self):
return len(self.itemList)
def itemAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList[index]
return None
def takeAt(self, index):
if index >= 0 and index < len(self.itemList):
return self.itemList.pop(index)
return None
def expandingDirections(self):
return Qt.Orientations(Qt.Orientation(0))
def hasHeightForWidth(self):
return True
def heightForWidth(self, width):
height = self.doLayout(QRect(0, 0, width, 0), True)
return height
def setGeometry(self, rect):
super(FlowLayout, self).setGeometry(rect)
self.doLayout(rect, False)
def sizeHint(self):
return self.minimumSize()
def minimumSize(self):
size = QSize()
for item in self.itemList:
size = size.expandedTo(item.minimumSize())
margin, _, _, _ = self.getContentsMargins()
size += QSize(2 * margin, 2 * margin)
return size
def doLayout(self, rect, testOnly):
x = rect.x()
y = rect.y()
lineHeight = 0
for item in self.itemList:
wid = item.widget()
spaceX = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton,
QSizePolicy.PushButton, Qt.Horizontal)
spaceY = self.spacing() + wid.style().layoutSpacing(QSizePolicy.PushButton,
QSizePolicy.PushButton, Qt.Vertical)
nextX = x + item.sizeHint().width() + spaceX
if nextX - spaceX > rect.right() and lineHeight > 0:
x = rect.x()
y = y + lineHeight + spaceY
nextX = x + item.sizeHint().width() + spaceX
lineHeight = 0
if not testOnly:
item.setGeometry(QRect(QPoint(x, y), item.sizeHint()))
x = nextX
lineHeight = max(lineHeight, item.sizeHint().height())
return y + lineHeight - rect.y()
if __name__ == '__main__':
import sys
app = QApplication(sys.argv)
mainWin = Window()
mainWin.show()
sys.exit(app.exec_())
到此這篇關(guān)于python GUI框架pyqt5 對(duì)圖片進(jìn)行流式布局的方法(瀑布流flowlayout)的文章就介紹到這了,更多相關(guān)python pyqt5圖片流式布局內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
基于Python實(shí)現(xiàn)通過(guò)微信搜索功能查看誰(shuí)把你刪除了
這篇文章主要介紹了基于Python實(shí)現(xiàn)微信搜索查看誰(shuí)把你刪除了的相關(guān)資料,需要的朋友可以參考下2016-01-01
python pytorch中.view()函數(shù)的用法解讀
這篇文章主要介紹了python pytorch中.view()函數(shù)的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
Python實(shí)現(xiàn)連接兩個(gè)無(wú)規(guī)則列表后刪除重復(fù)元素并升序排序的方法
這篇文章主要介紹了Python實(shí)現(xiàn)連接兩個(gè)無(wú)規(guī)則列表后刪除重復(fù)元素并升序排序的方法,涉及Python針對(duì)列表的合并、遍歷、判斷、追加、排序等操作技巧,需要的朋友可以參考下2018-02-02
解決Python3錯(cuò)誤:SyntaxError: unexpected EOF while
這篇文章主要介紹了解決Python3錯(cuò)誤:SyntaxError: unexpected EOF while parsin問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07
Python函數(shù)的定義方式與函數(shù)參數(shù)問(wèn)題實(shí)例分析
這篇文章主要介紹了Python函數(shù)的定義方式與函數(shù)參數(shù)問(wèn)題,結(jié)合實(shí)例形式詳細(xì)分析了Python函數(shù)定義、函數(shù)參數(shù)相關(guān)原理、操作技巧與注意事項(xiàng),需要的朋友可以參考下2019-12-12
詳解利用上下文管理器擴(kuò)展Python計(jì)時(shí)器
本文將和大家一起了解什么是上下文管理器?和?Python?的?with?語(yǔ)句,以及如何完成自定義。然后擴(kuò)展?Timer?以便它也可以用作上下文管理器,感興趣的可以了解一下2022-06-06
Python中關(guān)于浮點(diǎn)數(shù)的冷知識(shí)
這篇文章主要給大家介紹了Python中關(guān)于浮點(diǎn)數(shù)的冷知識(shí),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用Python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09

