pyqt5 布局 layout 收集

参考1:

https://www.jianshu.com/p/b39d3a17d902
绝对定位
每个程序都是以像素为单位区分元素的位置,衡量元素的大小。所以我们完全可以使用绝对定位搞定每个元素和窗口的位置。但是这也有局限性:

元素不会随着我们更改窗口的位置和大小而变化。
不能适用于不同的平台和不同分辨率的显示器
更改应用字体大小会破坏布局
如果我们决定重构这个应用,需要全部计算一下每个元素的位置和大小

参考2

https://www.jianshu.com/p/b39d3a17d902
如果我们需要把两个按钮放在程序的右下角,创建这样的布局,我们只需要一个水平布局加一个垂直布局的盒子就可以了。再用弹性布局增加一点间隙。
上面的例子完成了在应用的右下角放了两个按钮的需求。当改变窗口大小的时候,它们能依然保持在相对的位置。

#!/usr/bin/python3
# -*- coding: utf-8 -*-

"""
ZetCode PyQt5 tutorial 

In this example, we position two push
buttons in the bottom-right corner 
of the window. 

Author: Jan Bodnar
Website: zetcode.com 
Last edited: August 2017
"""

import sys
from PyQt5.QtWidgets import (QWidget, QPushButton, 
    QHBoxLayout, QVBoxLayout, QApplication)


class Example(QWidget):

    def __init__(self):
        super().__init__()

        self.initUI()


    def initUI(self):

        okButton = QPushButton("OK")
        cancelButton = QPushButton("Cancel")

        hbox = QHBoxLayout()
        hbox.addStretch(1)
        hbox.addWidget(okButton)
        hbox.addWidget(cancelButton)

        vbox = QVBoxLayout()
        vbox.addStretch(1)
        vbox.addLayout(hbox)

        self.setLayout(vbox)    

        self.setGeometry(300, 300, 300, 150)
        self.setWindowTitle('Buttons')    
        self.show()


if __name__ == '__main__':

    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
参考3

https://zhuanlan.zhihu.com/p/64574283
https://github.com/luochang212/pyqt5-demo/blob/master/Demo/praise_me_please.py

main_layout.setStretch(0, 1)表示0号部件的拉伸设置为1
main_layout.setStretch(1, 3)`表示1号部件的拉伸设置为3

参考4:

https://www.cnblogs.com/laizhenghong2012/p/10040798.html

绝对布局的缺点:

如果改变一个窗口的大小,窗口中控件的大小和位置不会随之改变
所生成的窗口在不同的操作系统下看起来可能不一样
在程序中改变字体时可能会破坏布局
如果修改布局,比如新增一个控件,就必须全部重新布局,既烦琐又费时

对齐方式Qt.Alignment可能取值有:

addWidget(self, QWidget, stretch, Qt.Alignment alignment) 在布局中添加控件,stretch为伸缩量,alignment为对齐方式
参数 描述
Qt.AlignLeft 水平居左对齐
Qt.AlignRight 水平居右对齐
Qt.AlignCenter 水平居中对齐
Qt.AlignJustify 水平两端对齐
Qt.AlignTop 垂直靠上对齐
Qt.AlignBottom 垂直靠下对齐
Qt.AlignVCenter 垂直居中对齐

#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import sys
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QWidget, QPushButton, QHBoxLayout, QApplication

class Window(QWidget):
    def __init__(self):
        super().__init__()

        hbox = QHBoxLayout()
        hbox.addWidget(QPushButton(str(1)), 0, Qt.AlignTop)
        hbox.addWidget(QPushButton(str(2)), 0, Qt.AlignTop | Qt.AlignLeft)
        hbox.addWidget(QPushButton(str(3)), 0)
        hbox.addWidget(QPushButton(str(4)), 0, Qt.AlignLeft | Qt.AlignBottom)
        hbox.addWidget(QPushButton(str(5)), 0, Qt.AlignTop)

        self.setLayout(hbox)
        self.setWindowTitle('水平布局')
        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    sys.exit(app.exec_())
参考5 :

https://blog.51cto.com/9291927/2423303
https://blog.51cto.com/9291927/2423303
PyQt5快速入门(五)PyQt5布局管理

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QVBoxLayout, QHBoxLayout, QPushButton, QSizePolicy

class MainWindow(QWidget):

    def __init__(self, parent=None):
        super().__init__(parent)
        self.layout = QVBoxLayout()
        self.layout.setSpacing(20)
        # 第一行按钮布局管理
        hLayout1 = QHBoxLayout()
        button = QPushButton("Button1")
        button.setMinimumSize(60, 30)
        button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        hLayout1.addWidget(button)
        button = QPushButton("Button2")
        button.setMinimumSize(60, 30)
        button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        hLayout1.addWidget(button)
        # 第二行按钮布局管理
        hLayout2 = QHBoxLayout()
        button = QPushButton("Button1")
        button.setMinimumSize(60, 30)
        button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        hLayout2.addWidget(button)
        button = QPushButton("Button2")
        button.setMinimumSize(60, 30)
        button.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        hLayout2.addWidget(button)
        # 整体垂直布局管理
        self.layout.addLayout(hLayout1)
        self.layout.addLayout(hLayout2)

        self.setLayout(self.layout)

if __name__ == "__main__":
    app = QApplication(sys.argv)
    window = MainWindow()
    window.resize(400, 200)
    window.show()

    sys.exit(app.exec_())
pyqt5 中文教程

http://code.py40.com/pyqt5/18.html

参考7

import sys
from PyQt5.QtWidgets import QApplication, QWidget, QHBoxLayout, QPushButton
from PyQt5.QtCore import Qt
class Winform(QWidget):
def init(self, parent=None):
super(Winform,self).init(parent)
self.setWindowTitle(“水平布局管理”)
self.resize(800,200)

    hlayout = QHbobLayout()
    #按钮水平居左,垂直居上
    hlayout.addWidget(QPushButton(str(1)),0, Qt.AlignLeft|Qt.AlignTop)
    hlayout.addWidget(QPushButton(str(2)),0, Qt.AlignLeft|Qt.AlignTop)
    hlayout.addWidget(QPushButton(str(3),5))#这里5表示的是权重
    #水平居左,垂直居下
    hlayout.addWidget(QPushButton(str(4)),0,Qt.AlignLeft|Qt.AlignBottom)
    hlayout.addWidget(QpushButton(str(5)),0, Qt.ALignLeft|Qt.AlignBottom)
    self.setLayout(hlayout)

if name == “main”:
app = QApplication(sys.argv)
form = Winform()
form.show()
sys.exit(app.exec_())

一篇文章让你读懂PyQt5布局管理,绝对干货

https://cloud.tencent.com/developer/article/1437295

Qt Designer 设计PyQt5界面时自适应设置

https://blog.csdn.net/loovelj/article/details/79529625?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromBaidu-1

Qt Creator 窗体控件自适应窗口大小布局

https://www.cnblogs.com/emouse/archive/2013/05/19/3087708.html

在这里插入图片描述
结合控件的SizePolicy属性,来控制布局管理中的控件的尺寸自适应方式。

控件的sizePolicy说明控件在布局管理中的缩放方式。Qt提供的控件都有一个合理的缺省sizePolicy,但是这个缺省值有时不能适合所有的布局,开发人员经常需要改变窗体上的某些控件的sizePolicy。一个QSizePolicy的所有变量对水平方向和垂直方向都适用。下面列举了一些最长用的值:

A. Fixed:控件不能放大或者缩小,控件的大小就是它的sizeHint。

B. Minimum:控件的sizeHint为控件的最小尺寸。控件不能小于这个sizeHint,但是可以

放大。

C. Maximum:控件的sizeHint为控件的最大尺寸,控件不能放大,但是可以缩小到它的最小

的允许尺寸。

D. Preferred:控件的sizeHint是它的sizeHint,但是可以放大或者缩小

E. Expandint:控件可以自行增大或者缩小

注:sizeHint(布局管理中的控件默认尺寸,如果控件不在布局管理中就为无效的值)

在这里插入图片描述

pyqt窗口控件跟随窗口大小变化而变化的方法

https://blog.csdn.net/zerfew/article/details/84107875?depth_1-utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3&utm_source=distribute.pc_relevant.none-task-blog-BlogCommendFromMachineLearnPai2-3
用designer时要注意,不要选中多个控件然后右键选择layout,要直接在mainwindow的widget,然后右键layout,一般选择网格布局,即可实现所有控件的排布,再根据需求,对控件大小进行特殊配置即可.

启用页面布局时,软件会自动排版,如果对排版不满意,可以对每个控件的长和宽的最小值和最大值进行配置,从而实现对页面布局的重新排版.比如不希望某个控件过大,就可以设置其最大长和宽为多少.如果希望不希望某个控件太小,就配置其最小的长和宽为多少.

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值