我正在尝试在布局中添加几个标签,并且这些项目必须相邻。
以下代码使它们相邻,但位于布局的中间。我希望这些标签在顶部。
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()
hlayout = QHBoxLayout()
widget = QWidget()
widget.setLayout(hlayout)
a1 = QLabel('label1')
a2 = QLabel('label2')
hlayout.addWidget(a1)
hlayout.addWidget(a2)
hlayout.addStretch(2)
vlayout.addLayout(hlayout)
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_())