国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

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

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

流式布局

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

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

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

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

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

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

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 视频二区精品中文字幕 | 91精品欧美综合在线观看 | 韩日一级毛片 | 91香蕉国产线在线观看免费 | 精品成人免费一区二区在线播放 | 91久热| 久久只有精品视频 | 日本乱人伦在线观看免费 | 国产夫妻视频 | 中文字幕区 | 国产午夜精品一区二区三区不卡 | 免费国产成人高清在线观看不卡 | 欧美成人午夜不卡在线视频 | 久久www免费人成_看片高清 | 国产成人精品男人的天堂538 | 欧美成人激情在线 | 久久国产影视 | 久久国产乱子伦精品免费不卡 | 日韩不卡一二三区 | 国产三级香港三韩国三级 | 日本色哟哟 | 免费看a网站 | 免费国产成人综合 | 国产精品成人在线 | 亚洲视频免费播放 | 国产三级理论 | vr欧美乱强伦xxxxx | 亚洲精品成人久久久影院 | 国产成人ay手机在线观看 | 在线欧美视频 | 亚洲一区二区三区91 | 欧美日韩一区二区三区视频在线观看 | 成人免费高清视频 | 手机看片免费基地 | 欧洲一级视频 | a级毛片免费高清毛片视频 a级毛片免费高清视频 | 国产精品免费一级在线观看 | 午夜视频网站 | 久草一级片| 国产精品亚洲玖玖玖在线靠爱 | 国产精品亚洲专区在线播放 |