色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

您的位置:首頁技術文章
文章詳情頁

python GUI框架pyqt5 對圖片進行流式布局的方法(瀑布流flowlayout)

瀏覽:47日期:2022-08-02 16:20:43

流式布局

流式布局,也叫做瀑布流布局,是網頁中經常使用的一種頁面布局方式,它的原理就是將高度固定,然后圖片的寬度自適應,這樣加載出來的圖片看起來就像瀑布一樣整齊的水流淌下來。

pyqt流式布局

那么在pyqt5中我們怎么使用流式布局呢?pyqt沒有這個控件,需要我們自己去封裝,下面是流式布局的封裝代碼。

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()

封裝好的流式布局類,我們只要傳入相應的layout之后,他就會自動計算頁面的元素,適應頁面的寬度。

下面是我們寫的一個瀑布流顯示圖片的代碼:

from PyQt5.QtCore import QPoint, QRect, QSize, Qtimport osfrom PyQt5 import QtCore, QtGui, QtWidgetsfrom 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 + spaceYnextX = x + item.sizeHint().width() + spaceXlineHeight = 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_())

到此這篇關于python GUI框架pyqt5 對圖片進行流式布局的方法(瀑布流flowlayout)的文章就介紹到這了,更多相關python pyqt5圖片流式布局內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 特级aa毛片在线播放 | 免费的成人a视频在线观看 免费的毛片 | 超清波多野结衣精品一区 | 欧美中文在线 | 男女午夜 | 青青自拍 | 亚洲色色色图 | 国产精品免费看久久久 | 另类在线视频 | 欧美一级www片免费观看 | 久久只有这才是精品99 | 国产成人综合95精品视频免费 | 免费看毛片网 | 国产真实一区二区三区 | 亚洲综合精品一二三区在线 | 免费看91毛片 | 国产精品久久久久影视不卡 | 操操操网 | 精品久久久久久影院免费 | 欧美高清在线精品一区二区不卡 | 91精品欧美一区二区三区 | 孕妇交| 久久国内精品自在自线400部o | 波多野结衣在线看片 | 国产成人综合日韩精品无 | 欧美成人一区二区三区 | 综合 欧美 亚洲日本 | 欧美高清不卡 | 三级黄色在线播放 | 久久久久久亚洲精品 | 国产成人综合亚洲亚洲欧美 | 欧美黄色特级视频 | 亚洲人在线 | 欧美一区精品 | 亚洲在线播放视频 | 久久视频国产 | 免费看成人www的网站软件 | 久久久成人啪啪免费网站 | 免费一级毛片在播放视频 | 999国产精品亚洲77777 | 国产成人精品视频免费 |