简体   繁体   English

减少 PyQt5 中的间距 - GridLayout - 小部件之间

[英]Reduce spacing in PyQt5 - GridLayout - Between the widgets

I am new to PyQt5, I created a grid layout as shown in figure I want to reduce the gap between the three widgets, I tried playing around margins, spacing and row stretch but none have worked, Please look ar the image in hyperlink and help me:我是 PyQt5 的新手,我创建了一个如图所示的网格布局我想减少三个小部件之间的差距,我尝试了边距、间距和行拉伸,但都没有奏效,请查看超链接中的图像并帮助我:

Image:图片: 图片

def createlayout(self):
    self.label1=QLabel(self.label,self)
    self.label2=QLabel(self.label2,self)
    self.label3 = QLabel("try", self)
    self.textbox = QLineEdit(self)

    vbox=QGridLayout()


    vbox.addWidget(self.label1,0,0,1,1)

    vbox.addWidget(self.textbox,1,0,1,1)

    vbox.addWidget(self.label2,2,0,1,1)

    vbox.addWidget(self.label3, 3, 0, 1, 1)

    vbox.setContentsMargins(1,0,0,0)
    #vbox.setAlignment('AlignCenter')
    vbox.setRowStretch(0, 0)
    vbox.setRowStretch(1, 0)
    vbox.setRowStretch(2,0)
    vbox.setColumnStretch(1,0)
    #vbox.setRowStretch(2,1)
    vbox.setRowStretch(3,0)
    vbox.setSpacing(0)

QGridLayout::setRowStretch(int row, int stretch) QGridLayout::setRowStretch(int row, int stretch)

Sets the stretch factor of row row to stretch.将行行的拉伸因子设置为拉伸。 The first row is number 0.第一行是数字 0。

The stretch factor is relative to the other rows in this grid.拉伸因子相对于该网格中的其他行。 Rows with a higher stretch factor take more of the available space.具有较高拉伸因子的行占用更多可用空间。

The default stretch factor is 0. If the stretch factor is 0 and no other row in this table can grow at all, the row may still grow.默认拉伸因子为 0。如果拉伸因子为 0,并且此表中的其他行根本无法增长,则该行可能仍会增长。

import sys
from PyQt5.Qt import *

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

    def createlayout(self):
        self.label1 = QLabel("self.label")
        self.label2 = QLabel("self.label2")
        self.label3 = QLabel("try", )
        self.textbox = QLineEdit()

        vbox = QGridLayout(self)
        vbox.addWidget(self.label1, 0, 0)
        vbox.addWidget(self.textbox, 1, 0)
        vbox.addWidget(self.label2, 2, 0)
        vbox.addWidget(self.label3, 3, 0)
        
        vbox.setRowStretch(4, 1)                                 # +++

        
if __name__ == '__main__':
    app = QApplication(sys.argv)
    w = Window()
    w.resize(300, 200)
    w.show()
    sys.exit(app.exec_())

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM