删除QLabel和QLineEdit之间的边距

时间:2020-02-18 02:15:30

标签: python pyside2

我正在尝试创建登录窗口,该窗口在QLabel下具有QLineEdit,但是当前的QLabel在Window中占用了太多空间,我不知道为什么,这是图片:

QLabel { background-color: red; }

red

我的代码:

self.text = QLabel("LOGIN")

# self.text.setStyleSheet("QLabel { background-color: red; color : white;margin-top: 50px;margin-bottom: 0px; }")
self.text.setAlignment(Qt.AlignCenter)
# self.text.setContentsMargins(0, 0, 0, 0)
# self.text.setGeometry(QRect(10,10,30,80))

font = QFont("Sans-Serif", 30)
self.text.setFont(font)

form = QLineEdit("Write my name here..")
form.setAlignment(Qt.AlignCenter)
# form.setAlignment(Qt.AlignHCenter)

self.layout = QVBoxLayout()
# self.layout.setContentsMargins(0, 0, 0, 0)
self.layout.setSpacing(0)
self.layout.addWidget(self.text)
self.layout.addWidget(form)
self.layout.addWidget(self.button)
self.setLayout(self.layout)

enter image description here

1 个答案:

答案 0 :(得分:0)

我没有看到OP提供的代码与要从中复制结构的LOGIN窗口之间的关系,因此请从头开始创建以下代码。

from PySide2 import QtCore, QtGui, QtWidgets


class LoginPage(QtWidgets.QWidget):
    def __init__(self, parent=None):
        super().__init__(parent)

        self.image_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
        self.login_label = QtWidgets.QLabel(
            text=self.tr("LOG IN"), alignment=QtCore.Qt.AlignCenter
        )
        self.email_username_lineedit = QtWidgets.QLineEdit(
            placeholderText=self.tr("Email or username")
        )
        self.password_lineedit = QtWidgets.QLineEdit(
            placeholderText=self.tr("Password"), echoMode=QtWidgets.QLineEdit.Password
        )
        self.enter_button = QtWidgets.QPushButton(self.tr("Enter"))
        self.forgot_password_label = QtWidgets.QLabel(
            self.tr("Forgot password?"), alignment=QtCore.Qt.AlignCenter
        )

        self.image_label.setPixmap(QtGui.QPixmap("so-icon.png"))

        # font = self.
        font = self.login_label.font()
        font.setPointSize(30)
        self.login_label.setFont(font)

        self.login_label.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed
        )
        self.forgot_password_label.setSizePolicy(
            QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Fixed
        )

        lay = QtWidgets.QVBoxLayout(self)
        lay.setSpacing(0)
        lay.addWidget(self.image_label)
        lay.addWidget(self.login_label)
        lay.addWidget(self.email_username_lineedit)
        lay.addWidget(self.password_lineedit)
        lay.addWidget(self.enter_button)
        lay.addWidget(self.forgot_password_label)

        self.resize(320, 480)


if __name__ == "__main__":
    import sys

    app = QtWidgets.QApplication(sys.argv)
    w = LoginPage()
    w.show()
    sys.exit(app.exec_())

enter image description here

相关问题