gpt4 book ai didi

pyqt5格式化使用多种布局

转载 作者:行者123 更新时间:2023-12-05 06:32:02 24 4
gpt4 key购买 nike

我有 4 个标签,第一行需要第 1 个 2 个表,第二行需要 2 个。

下面的代码工作正常,但该行跨越了整个屏幕。

import sys

from PyQt5.QtWidgets import QWidget, QApplication, QDialog, QGridLayout, QLabel, QLineEdit
from PyQt5.Qt import QHBoxLayout, QWindow, QMainWindow, QVBoxLayout


class Example(QMainWindow):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.initUI()

def initUI(self):
vlayout = QVBoxLayout()
hlayout1 = QHBoxLayout()
hlayout2 = QHBoxLayout()
widget = QWidget()
widget.setLayout(vlayout)

a1 = QLabel('label1')
a2 = QLabel('label2')
hlayout1.addWidget(a1)
hlayout1.addWidget(a2)
hlayout1.addStretch(2)
vlayout.addLayout(hlayout1)
vlayout.addStretch(1)
a3 = QLabel('label3')
a4 = QLabel('label3')
hlayout2.addWidget(a3)
hlayout2.addWidget(a4)
hlayout2.addStretch(1)
vlayout.addLayout(hlayout2)
vlayout.addStretch(1)

self.setCentralWidget(widget)

self.setGeometry(500, 500, 500, 500)
self.setWindowTitle('Lines')
self.show()


if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
# ex.show()
sys.exit(app.exec_())

最佳答案

删除行 vlayout.addStretch(1)

import sys

from PyQt5.QtWidgets import QWidget, QApplication, QDialog, QGridLayout, QLabel, QLineEdit
from PyQt5.Qt import QHBoxLayout, QWindow, QMainWindow, QVBoxLayout


class Example(QMainWindow):
def __init__(self, *args, **kwargs):
super(Example, self).__init__(*args, **kwargs)
self.initUI()

def initUI(self):
vlayout = QVBoxLayout()
hlayout1 = QHBoxLayout()
hlayout2 = QHBoxLayout()
widget = QWidget()
widget.setLayout(vlayout)

a1 = QLabel('label1')
a2 = QLabel('label2')
hlayout1.addWidget(a1)
hlayout1.addWidget(a2)
hlayout1.addStretch(2)
vlayout.addLayout(hlayout1)
#vlayout.addStretch(1) # < ----
a3 = QLabel('label3')
a4 = QLabel('label3')
hlayout2.addWidget(a3)
hlayout2.addWidget(a4)
hlayout2.addStretch(1)
vlayout.addLayout(hlayout2)
vlayout.addStretch(1)

self.setCentralWidget(widget)

self.setGeometry(500, 500, 500, 500)
self.setWindowTitle('Lines')
self.show()


if __name__ == '__main__':

app = QApplication(sys.argv)
ex = Example()
# ex.show()
sys.exit(app.exec_())

enter image description here

关于pyqt5格式化使用多种布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51541649/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com