布局没有扩展到整个风

2024-10-08 18:30:53 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在编写一个简单的加载屏幕,以备以后需要。我已经有一段时间没有使用PyQt了,现在一切都在尝试和失败中进行。我想要一个扩展到边界尽头的QProgressBar。但我得到的却是:

You can see the very thin progressbar not reaching the right border of the layout.

这让我觉得是布局没有扩大。我想要你的帮助和经验!谢谢!你知道吗

我的代码:

class LoadingScren(QWidget):

    def __init__(self):
        super().__init__()

        self.windowWidth = 500
        self.windowHeight = 300
        self.backgroundImage = 'bg.jpg'
        self.icon = 'template_icon.png'
        self.iconWidht = 100
        self.iconHeight = 100
        self.headline = 'Headline'
        self.description = """Lorem ipsum dolor sit amet"""

        self.initUI()

    def initUI(self):
        self.setFixedSize(self.windowWidth, self.windowHeight)
        self.setWindowFlag(Qt.FramelessWindowHint)

        imgIcon = QLabel()
        imgIcon.setPixmap(QPixmap(self.icon).scaled(self.iconWidht, self.iconHeight))

        lblHeadline = QLabel()
        lblHeadline.setText(self.headline)
        lblHeadline.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        lblHeadline.setStyleSheet('font-size: 75px')

        lblDescription = QLabel()
        lblDescription.setScaledContents(True)
        lblDescription.setText(self.description)
        lblDescription.setStyleSheet('font-size: 25px')

        progressBar = QProgressBar()
        progressBar.setMaximumHeight(2)
        progressBar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        headerLayout = QHBoxLayout()
        headerLayout.addWidget(imgIcon)
        headerLayout.addWidget(lblHeadline)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(headerLayout)
        mainLayout.addWidget(lblDescription)
        mainLayout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
        mainLayout.addWidget(progressBar)

        self.setLayout(mainLayout)
        self.show()

Tags: selfinitdeficonprogressbaraddwidgetqlabelqsizepolicy
1条回答
网友
1楼 · 发布于 2024-10-08 18:30:53

使用progressBar.setTextVisible(False)

或者

Qt样式表progressBar.setStyleSheet(...)

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui     import *
from PyQt5.QtCore    import *

class LoadingScren(QWidget):

    def __init__(self):
        super().__init__()

        self.windowWidth  = 500
        self.windowHeight = 300
        #self.backgroundImage = 'bg.jpg'            
        self.icon = 'qt-logo.png'            #'template_icon.png'
        self.iconWidht  = 100
        self.iconHeight = 100
        self.headline = 'Headline'
        self.description = """Lorem ipsum dolor sit amet"""

        self.initUI()

    def initUI(self):
        self.setFixedSize(self.windowWidth, self.windowHeight)
        self.setWindowFlag(Qt.FramelessWindowHint)

        imgIcon = QLabel()
        imgIcon.setPixmap(QPixmap(self.icon).scaled(self.iconWidht, self.iconHeight))

        lblHeadline = QLabel()
        lblHeadline.setText(self.headline)
        lblHeadline.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)
        lblHeadline.setStyleSheet('font-size: 75px')

        lblDescription = QLabel()
        lblDescription.setScaledContents(True)
        lblDescription.setText(self.description)
        lblDescription.setStyleSheet('font-size: 25px')

        progressBar = QProgressBar()
        progressBar.setMaximumHeight(2)
        progressBar.setSizePolicy(QSizePolicy.Expanding, QSizePolicy.Expanding)

        progressBar.setTextVisible(False)
        # or
        #progressBar.setStyleSheet("""QProgressBar {
        #                              border: 1px solid red;
        #                         }""")

        headerLayout = QHBoxLayout()
        headerLayout.addWidget(imgIcon)
        headerLayout.addWidget(lblHeadline)

        mainLayout = QVBoxLayout()
        mainLayout.addLayout(headerLayout)
        mainLayout.addWidget(lblDescription)
        mainLayout.addItem(QSpacerItem(20, 40, QSizePolicy.Minimum, QSizePolicy.Expanding))
        mainLayout.addWidget(progressBar)

        self.setLayout(mainLayout)
        self.show()

if __name__ == '__main__':
    app     = QApplication(sys.argv)
    mainWnd = LoadingScren()
    sys.exit(app.exec_())

enter image description here

enter image description here

相关问题 更多 >

    热门问题