热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python流式计算框架_pythonGUI框架pyqt5如何对图片进行流式布局(瀑布流flowlayout)...

流式布局流式布局,也叫做瀑布流布局,是网页中经常使用的一种页面布局方式,它的原理就是将高度固定,然后图片的宽度自适应

流式布局

流式布局,也叫做瀑布流布局,是网页中经常使用的一种页面布局方式,它的原理就是将高度固定,然后图片的宽度自适应,这样加载出来的图片看起来就像瀑布一样整齐的水流淌下来。

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

return self.itemList[index]

return None

def takeAt(self, index):

if index >= 0 and index

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, 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

return self.itemList[index]

return None

def takeAt(self, index):

if index >= 0 and index

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



推荐阅读
  • 本文介绍了pack布局管理器在Perl/Tk中的使用方法及注意事项。通过调用pack()方法,可以控制部件在显示窗口中的位置和大小。同时,本文还提到了在使用pack布局管理器时,应注意将部件分组以便在水平和垂直方向上进行堆放。此外,还介绍了使用Frame部件或Toplevel部件来组织部件在窗口内的方法。最后,本文强调了在使用pack布局管理器时,应避免在中间切换到grid布局管理器,以免造成混乱。 ... [详细]
  • 展开全部下面的代码是创建一个立方体Thisexamplescreatesanddisplaysasimplebox.#Thefirstlineloadstheinit_disp ... [详细]
  • 个人学习使用:谨慎参考1Client类importcom.thoughtworks.gauge.Step;importcom.thoughtworks.gauge.T ... [详细]
  • Android开发实现的计时器功能示例
    本文分享了Android开发实现的计时器功能示例,包括效果图、布局和按钮的使用。通过使用Chronometer控件,可以实现计时器功能。该示例适用于Android平台,供开发者参考。 ... [详细]
  • 本文介绍了在处理不规则数据时如何使用Python自动提取文本中的时间日期,包括使用dateutil.parser模块统一日期字符串格式和使用datefinder模块提取日期。同时,还介绍了一段使用正则表达式的代码,可以支持中文日期和一些特殊的时间识别,例如'2012年12月12日'、'3小时前'、'在2012/12/13哈哈'等。 ... [详细]
  • 模板引擎StringTemplate的使用方法和特点
    本文介绍了模板引擎StringTemplate的使用方法和特点,包括强制Model和View的分离、Lazy-Evaluation、Recursive enable等。同时,还介绍了StringTemplate语法中的属性和普通字符的使用方法,并提供了向模板填充属性的示例代码。 ... [详细]
  • 本文介绍了利用ARMA模型对平稳非白噪声序列进行建模的步骤及代码实现。首先对观察值序列进行样本自相关系数和样本偏自相关系数的计算,然后根据这些系数的性质选择适当的ARMA模型进行拟合,并估计模型中的位置参数。接着进行模型的有效性检验,如果不通过则重新选择模型再拟合,如果通过则进行模型优化。最后利用拟合模型预测序列的未来走势。文章还介绍了绘制时序图、平稳性检验、白噪声检验、确定ARMA阶数和预测未来走势的代码实现。 ... [详细]
  • Python教学练习二Python1-12练习二一、判断季节用户输入月份,判断这个月是哪个季节?3,4,5月----春 ... [详细]
  • 从批量eml文件中提取附件的Python代码实现方法
    本文介绍了使用Python代码从批量eml文件中提取附件的实现方法,包括获取eml附件信息、递归文件夹下所有文件、创建目的文件夹等步骤。通过该方法可以方便地提取eml文件中的附件,并保存到指定的文件夹中。 ... [详细]
  • 我用Tkinter制作了一个图形用户界面,有两个主按钮:“开始”和“停止”。请您就如何使用“停止”按钮终止“开始”按钮为以下代码调用的已运行功能提供建议 ... [详细]
  • vb.net不用多线程如何同时运行两个过程?不用多线程?即使用多线程,也不会是“同时”执行,题主只要略懂一些计算机编译原理就能明白了。不用多线程更不可能让两个过程同步执行了。不过可 ... [详细]
  • 第一步:PyQt4Designer设计程序界面该部分设计类同VisvalStudio内的设计,改下各部件的objectName!设计 ... [详细]
  • 本文整理了Java面试中常见的问题及相关概念的解析,包括HashMap中为什么重写equals还要重写hashcode、map的分类和常见情况、final关键字的用法、Synchronized和lock的区别、volatile的介绍、Syncronized锁的作用、构造函数和构造函数重载的概念、方法覆盖和方法重载的区别、反射获取和设置对象私有字段的值的方法、通过反射创建对象的方式以及内部类的详解。 ... [详细]
  • 单页面应用 VS 多页面应用的区别和适用场景
    本文主要介绍了单页面应用(SPA)和多页面应用(MPA)的区别和适用场景。单页面应用只有一个主页面,所有内容都包含在主页面中,页面切换快但需要做相关的调优;多页面应用有多个独立的页面,每个页面都要加载相关资源,页面切换慢但适用于对SEO要求较高的应用。文章还提到了两者在资源加载、过渡动画、路由模式和数据传递方面的差异。 ... [详细]
  • HashMap的相关问题及其底层数据结构和操作流程
    本文介绍了关于HashMap的相关问题,包括其底层数据结构、JDK1.7和JDK1.8的差异、红黑树的使用、扩容和树化的条件、退化为链表的情况、索引的计算方法、hashcode和hash()方法的作用、数组容量的选择、Put方法的流程以及并发问题下的操作。文章还提到了扩容死链和数据错乱的问题,并探讨了key的设计要求。对于对Java面试中的HashMap问题感兴趣的读者,本文将为您提供一些有用的技术和经验。 ... [详细]
author-avatar
黄燕2602917715_290
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有