欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2020年03月12日 09:56:41   作者:dreamfly  
這篇文章主要介紹了python GUI框架pyqt5 對圖片進行流式布局的方法(瀑布流flowlayout),本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下

流式布局

流式布局,也叫做瀑布流布局,是網(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, 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 對圖片進行流式布局的方法(瀑布流flowlayout)的文章就介紹到這了,更多相關(guān)python pyqt5圖片流式布局內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python忽略警告(warning)的3種方法小結(jié)

    python忽略警告(warning)的3種方法小結(jié)

    python開發(fā)中經(jīng)常遇到報錯的情況,但是warning通常并不影響程序的運行,而且有時特別討厭,下面我們來說下如何忽略warning錯誤,這篇文章主要給大家介紹了關(guān)于python忽略警告(warning)的3種方法,需要的朋友可以參考下
    2023-10-10
  • Windows8下安裝Python的BeautifulSoup

    Windows8下安裝Python的BeautifulSoup

    這篇文章主要介紹了Windows8下安裝Python的BeautifulSoup,本文著重講解安裝中出現(xiàn)的錯誤和解決方法,需要的朋友可以參考下
    2015-01-01
  • python Socket網(wǎng)絡(luò)編程實現(xiàn)C/S模式和P2P

    python Socket網(wǎng)絡(luò)編程實現(xiàn)C/S模式和P2P

    這篇文章主要介紹了python Socket網(wǎng)絡(luò)編程實現(xiàn)C/S模式和P2P,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-06-06
  • Python2.7下安裝Scrapy框架步驟教程

    Python2.7下安裝Scrapy框架步驟教程

    本篇文章主要介紹了Python2.7下安裝Scrapy框架步驟教程,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-12-12
  • python去除所有html標簽的方法

    python去除所有html標簽的方法

    這篇文章主要介紹了python去除所有html標簽的方法,涉及Python正則替換的相關(guān)技巧,非常簡單實用,需要的朋友可以參考下
    2015-05-05
  • Python中super的用法實例

    Python中super的用法實例

    這篇文章主要介紹了Python中super的用法實例,本文對比了普通繼承和super繼承的相關(guān)內(nèi)容,從運行結(jié)果上看,普通繼承和super繼承是一樣的,但是其實它們的內(nèi)部運行機制不一樣,這一點在多重繼承時體現(xiàn)得很明顯,需要的朋友可以參考下
    2015-05-05
  • 用python + openpyxl處理excel2007文檔思路以及心得

    用python + openpyxl處理excel2007文檔思路以及心得

    最近要幫做RA的老姐寫個合并excel工作表的腳本……源數(shù)據(jù)是4000+個excel 工作表,分布在9個xlsm文件里,文件內(nèi)容是中英文混雜的一些數(shù)據(jù),需要從每張表中提取需要的部分,分門別類合并到多個大的表里。
    2014-07-07
  • python判斷一個變量是否已經(jīng)設(shè)置的方法

    python判斷一個變量是否已經(jīng)設(shè)置的方法

    這篇文章主要介紹了python判斷一個變量是否已經(jīng)設(shè)置的方法,有需要的朋友們可以跟著學(xué)習(xí)參考下。
    2020-08-08
  • 利用python在excel中畫圖的實現(xiàn)方法

    利用python在excel中畫圖的實現(xiàn)方法

    這篇文章主要介紹了利用python在excel中畫圖的實現(xiàn)方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • Python+seaborn實現(xiàn)聯(lián)合分布圖的繪制

    Python+seaborn實現(xiàn)聯(lián)合分布圖的繪制

    聯(lián)合分布(Joint Distribution)圖是一種查看兩個或兩個以上變量之間兩兩相互關(guān)系的可視化圖,在數(shù)據(jù)分析操作中經(jīng)常需要用到。本文將通過seaborn實現(xiàn)繪制聯(lián)合分布圖,需要的可以參考一下
    2023-02-02

最新評論