PyQt5修复Qlabel位置

时间:2018-08-15 14:53:44

标签: pyqt pyqt5 grid-layout qlabel

我是PyQt5的新手,我尝试为按钮创建带有网格布局的窗口。但是我想在窗口的左上角和右下角添加两个标签。

from PyQt5 import QtWidgets
from PyQt5 import QtCore
import sys

class PrettyWidget(QtWidgets.QWidget):

    def __init__(self):
        super(PrettyWidget, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(600,300, 1000, 600)
        self.setWindowTitle('Program v1.0')

        # Grid Layout
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)

        self.lbl1 = QtWidgets.QLabel(self)
        self.lbl1.setText('Author Information and Copy Right')
        self.lbl1.adjustSize()
        self.lbl1.move(588, 0)

        # Label indicator
        self.lbl2 = QtWidgets.QLabel(self)
        self.lbl2.setText('Click import to start...')
        self.lbl2.adjustSize()
        self.lbl2.move(0, 0)

        # Import data Button
        btn1 = QtWidgets.QPushButton('Select Data', self)
        btn1.resize(btn1.sizeHint())
        btn1.clicked.connect(self.getData)
        grid.addWidget(btn1, 0, 0)

        # Import names Button
        btn2 = QtWidgets.QPushButton('Select Names', self)
        btn2.resize(btn2.sizeHint())
        btn2.clicked.connect(self.getNames)
        grid.addWidget(btn2, 0, 1)

        # Run Button
        btn3 = QtWidgets.QPushButton('Run', self)
        btn3.resize(btn3.sizeHint())
        btn3.clicked.connect(self.Run)
        grid.addWidget(btn3, 1, 0)

        # Save Button
        btn4 = QtWidgets.QPushButton('Save',self)
        btn4.resize(btn4.sizeHint())
        btn4.clicked.connect(self.Save)
        grid.addWidget(btn4, 1, 1)

        self.show()

    def getData(self):
        self.lbl2.setText('Data selected!')
        self.lbl2.adjustSize()

    def getNames(self):
        self.lbl2.setText('Names selected!')
        self.lbl2.adjustSize()

    def Run(self):
        self.lbl2.setText('Done!')
        self.lbl2.adjustSize()

    def Save(self):
        self.lbl2.setText('Saved!')
        self.lbl2.adjustSize()

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = PrettyWidget()
    app.exec_()

if __name__ == '__main__':
    main()

如您所见,我现在对两个标签使用绝对位置。因此,当我最大化或更改窗口大小时,标签将停留在同一位置。如何像往常一样将lbl1粘在右下角,lbl粘在左上角?

1 个答案:

答案 0 :(得分:0)

将标签粘贴到布局中。 将行row的拉伸因子设置为stretch

import sys
from PyQt5 import QtWidgets
from PyQt5 import QtCore

class PrettyWidget(QtWidgets.QWidget):
    def __init__(self):
        super(PrettyWidget, self).__init__()
        self.initUI()

    def initUI(self):
        self.setGeometry(300,100, 1000, 600)
        self.setWindowTitle('Program v1.0')

        # Grid Layout
        grid = QtWidgets.QGridLayout()
        self.setLayout(grid)

        self.lbl1 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignLeft | QtCore.Qt.AlignVCenter)
        self.lbl1.setText('Author Information and Copy Right')
        self.lbl1.adjustSize()
        grid.addWidget(self.lbl1, 0, 0)

        grid.setRowStretch(1, 1)             # <---- Sets the stretch factor of row row to stretch .

        # Label indicator
        self.lbl2 = QtWidgets.QLabel(self, alignment=QtCore.Qt.AlignRight | QtCore.Qt.AlignVCenter)
        self.lbl2.setText('Click import to start...')
        self.lbl2.adjustSize()
        grid.addWidget(self.lbl2, 5, 1)

        # Import data Button
        btn1 = QtWidgets.QPushButton('Select Data', self)
        btn1.resize(btn1.sizeHint())
        btn1.clicked.connect(self.getData)
        grid.addWidget(btn1, 2, 0)
        # Import names Button
        btn2 = QtWidgets.QPushButton('Select Names', self)
        btn2.resize(btn2.sizeHint())
        btn2.clicked.connect(self.getNames)
        grid.addWidget(btn2, 2, 1)

        # Run Button
        btn3 = QtWidgets.QPushButton('Run', self)
        btn3.resize(btn3.sizeHint())
        btn3.clicked.connect(self.Run)
        grid.addWidget(btn3, 3, 0)
        # Save Button
        btn4 = QtWidgets.QPushButton('Save',self)
        btn4.resize(btn4.sizeHint())
        btn4.clicked.connect(self.Save)
        grid.addWidget(btn4, 3, 1)

        grid.setRowStretch(4, 1)          # <---- Sets the stretch factor of row row to stretch .

        self.show()

    def getData(self):
        self.lbl2.setText('Data selected!')
        self.lbl2.adjustSize()

    def getNames(self):
        self.lbl2.setText('Names selected!')
        self.lbl2.adjustSize()

    def Run(self):
        self.lbl2.setText('Done!')
        self.lbl2.adjustSize()

    def Save(self):
        self.lbl2.setText('Saved!')
        self.lbl2.adjustSize()

def main():
    app = QtWidgets.QApplication(sys.argv)
    w = PrettyWidget()
    app.exec_()

if __name__ == '__main__':
    main()

enter image description here