我无法将徽标在此 PyQt 窗口居中

问题描述 投票:0回答:1

我将中央 QWidget 分为两侧:左和右。左侧包含徽标,右侧小部件包含登录框。

因为我希望登录框具有固定大小并且距右侧边距一定距离,所以我包含了一个 QSpacerItem 来实现所需的行为。

但是,将 QSpacer 添加到右侧框架后,我的徽标被推离中心。为了平衡这一点,我也在左侧框架中添加了一个 QSpacer。

当我运行代码时,一切都按预期工作,但是当我展开屏幕时,徽标被推到右侧。我在徽标右侧添加了第二个 QSpacer 将其夹在中间,但我仍然无法使徽标居中。

当垫片仅添加到 right_side_frame 时:

when the spacer is only added to the right_side_frame

将徽标夹在两个垫片之间后的 GUI:

gui after sandwiching the logo between two spacers

我正在尝试重新创建:

What I'm trying to recreate

由于右框架内有空间,徽标在整个窗口中看起来并不居中

#Login Window
from PyQt6.QtWidgets import *
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt
from ColorWidget import Color

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

    # Window Properties
        self.setWindowTitle('login window')
        self.setMinimumSize(1050, 650)

    #Main Layouts
        main_layout = QHBoxLayout()

        left_side_frame = QWidget()
        right_side_frame = QWidget()


        main_layout.addWidget(left_side_frame)
        main_layout.addWidget(right_side_frame)

        
    #Left Layout
        left_layout = QHBoxLayout()

        left_h_spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
        left_layout.addItem(left_h_spacer)

        #label
        logo_label = QLabel('logo')
        logo_label.setAlignment(Qt.AlignmentFlag.AlignCenter)
        left_layout.addWidget(logo_label)

        left_side_frame.setLayout(left_layout)

        right_side_spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
        left_layout.addItem(right_side_spacer)


    #Right Layout

        right_layout = QHBoxLayout()


        #Sign-in Box
        signin_box = Color('white')
        signin_box.setObjectName('signin_box')
        signin_box.setFixedSize(450,550)

        Spacer = QSpacerItem(200, 20, QSizePolicy.Policy.Minimum, QSizePolicy.Policy.Expanding)
        right_layout.addItem(Spacer)
        right_layout.addWidget(signin_box)

        right_side_frame.setLayout(right_layout)
        right_side_frame.setContentsMargins(0, 50, 80, 50)

    
        # Set main widget
        canvas = QWidget()
        canvas.setObjectName('canvas')
        canvas.setLayout(main_layout)
        self.setCentralWidget(canvas)

if __name__ == '__main__':
    app = QApplication([])
    with open('style.css', 'r') as f:
        app.setStyleSheet(f.read())
    window = MainWindow()
    window.show()
    app.exec()

样式表:

#canvas {
    background-image: url('Images/test2.jpg');
    background-position: center;
}

#sign_in_label {
    color: black;
    font-size:  24px;
}
#email_label, #password_label, #register_label {
    color: grey;
    font-size: 12px;
}

我通过使用这个值在某种程度上使徽标居中:

left_h_spacer = QSpacerItem(500, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
        left_layout.addItem(left_h_spacer)

right_side_spacer = QSpacerItem(300, 20, QSizePolicy.Policy.Expanding, QSizePolicy.Policy.Minimum)
        left_layout.addItem(right_side_spacer)

我的屏幕尺寸是1512、982

python qt pyqt pyqt6
1个回答
0
投票

我能够解决这个问题。通过向 left_side_frame 添加拉伸因子 1,我能够保持徽标居中:

#Login Window
from PyQt6.QtWidgets import QHBoxLayout, QWidget, QVBoxLayout, QLabel, QLineEdit, QPushButton, QApplication, QMainWindow
from PyQt6.QtGui import QPixmap
from PyQt6.QtCore import Qt
from ColorWidget import Color

class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()

    # Window Properties
        self.setWindowTitle('window title')
        self.setMinimumSize(1050, 650)


    #Main Layouts
        main_layout = QHBoxLayout()

        left_side_frame = QWidget()
        right_side_frame = QWidget()

        main_layout.addWidget(left_side_frame,1)
        main_layout.addWidget(right_side_frame)

        
    #Left Layout
        left_layout = QHBoxLayout()

        #label
        logo_label = QLabel('logo')
        logo_label.setAlignment(Qt.AlignmentFlag.AlignCenter)

        left_layout.addWidget(logo_label)

        left_side_frame.setLayout(left_layout)


    #Right Layout

       #widgets and code for the right Layout

    # Set main widget
        canvas = QWidget()
        canvas.setObjectName('canvas')
        canvas.setLayout(main_layout)
        self.setCentralWidget(canvas)

if __name__ == '__main__':
    app = QApplication([])
    with open('style.css', 'r') as f:
        app.setStyleSheet(f.read())
    window = MainWindow()
    window.show()
    app.exec()
© www.soinside.com 2019 - 2024. All rights reserved.