यहां है MCVE (एक न्यूनतम, पूर्ण, सत्यापन योग्य उदाहरण) को सीधे बल्ले से चलना चाहिए

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QFormLayout, QGroupBox,QPushButton, QScrollArea


class Window(QMainWindow):

    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowTitle('MCVE')

        '''Window Background'''
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.black)
        self.setPalette(p)

        '''Core Layouts'''
        self.mainLayout = QGridLayout()
        self.picLayout = QHBoxLayout()
        self.redditSubs = QVBoxLayout()
        self.downloadBar = QHBoxLayout()
        self.panel = QWidget()
        self.profileInfo = QGridLayout()

        '''redditSubs Layout -----------------------------------------------------'''
        self.formLayout = QFormLayout()
        self.groupBox = QGroupBox()
        labelList = []
        favList = []
        favIcon = QPushButton('Favs')
        favIcon.setToolTip('<i><b>Add to favourites</i></b>')
        subscribedList = ['AccidentalRenaissance', 'HadToHurt', 'WidescreenWallpaper', 'iamverybadass',
                          'LivestreamFail', 'CatastrophicFailure', 'KidsAreFuckingStupid', 'therewasanattempt',
                          'BetterEveryLoop', 'madlads', 'IdiotsInCars', 'youseeingthisshit', 'blackmagicfuckery',
                          'WatchPeopleDieInside', 'SequelMemes', 'iamatotalpieceofshit', 'cleavesdropping',
                          'PewdiepieSubmissions', 'technicallythetruth', 'KeanuBeingAwesome', 'science', 'TikTokCringe',
                          'Cringetopia']
        for i in range(len(subscribedList)):
            labelList.append(QLabel('r/' + subscribedList[i]))
            favList.append(favIcon)
            self.formLayout.addRow(labelList[i], favList[i])
        self.groupBox.setLayout(self.formLayout)
        scroll = QScrollArea()
        scroll.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)
        scroll.setWidget(self.groupBox)
        scroll.setWidgetResizable(True)
        self.redditSubs.addWidget(scroll)
        self.groupBox.setObjectName('backgroundColor')
        self.groupBox.setStyleSheet('QLabel {color: #707070; font-size: 20px; padding-left: 10px; padding-top: 15px;}'
                                    'QGroupBox#backgroundColor {background-color: rgb(20, 21, 24); border: 1px solid black}'
                                    'QPushButton {text-align:center; font-size: 20px; padding-left: 10px; padding-top: 15px;}')

        '''------------------------------------------------------'''

        '''Nested Layout'''
        self.mainLayout.addLayout(self.profileInfo, 0, 0)
        self.mainLayout.addLayout(self.picLayout, 0, 1)
        self.mainLayout.addLayout(self.redditSubs, 1, 0)
        self.mainLayout.addLayout(self.downloadBar, 1, 1)

        '''Widgets'''
        self.display = QLabel('QHBoxLayout()')
        self.download = QLabel('QHBoxLayout()')
        self.subs = QLabel('QVBoxLayout()')
        self.fileInfo = QLabel('QGridLayout()')

        '''AddWidgets'''
        self.picLayout.addWidget(self.display)
        self.downloadBar.addWidget(self.download)
        self.redditSubs.addWidget(self.subs)
        self.mainLayout.addWidget(self.panel, 0, 0)
        self.profileInfo.addWidget(self.fileInfo, 0, 0)

        '''Stylesheet'''
        self.panel.setStyleSheet("background-color: red;")
        self.display.setStyleSheet('QLabel {color: white;}')
        self.download.setStyleSheet('QLabel {color: white;}')
        '''Initiating  mainLayout '''
        self.window = QWidget()
        self.window.setLayout(self.mainLayout)
        self.setCentralWidget(self.window)


if __name__ == '__main__':
    app = QApplication([])
    w = Window()
    w.showMaximized()
    app.exec_()

समस्याएं:

  1. लेबल एक के बाद एक जोड़े जा रहे हैं लेकिन बटन नहीं। मैंने favList.append(QPushButton('Favs')) किया जो ठीक काम करता है लेकिन इस तरह मैं आइकन नहीं जोड़ सकता।
  2. बटन का आकार बदलने में असमर्थ
  3. प्रपत्र लयआउट के चारों ओर की सीमा को हटाना प्रतीत नहीं होता।

जोड़ा गया: तस्वीर से पहले/बाद में

जोड़ा गया कोड:

from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton, QScrollArea


class Window(QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowTitle("MCVE")

        """Window Background"""
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.darkYellow)
        self.setPalette(p)

        """Core Layouts"""
        self.mainLayout = QGridLayout()
        self.picLayout = QHBoxLayout()
        self.redditSubs = QVBoxLayout()
        self.downloadBar = QHBoxLayout()
        self.profileInfo = QGridLayout()

        """Nested Layout"""
        self.mainLayout.addLayout(self.profileInfo, 0, 0)
        self.mainLayout.addLayout(self.picLayout, 0, 1)
        self.mainLayout.addLayout(self.redditSubs, 1, 0)
        self.mainLayout.addLayout(self.downloadBar, 1, 1)

        """Widgets"""
        self.display = QLabel("QHBoxLayout()")
        self.download = QLabel("QHBoxLayout()")
        self.subs = QLabel("QVBoxLayout()")
        self.fileInfo = QLabel("QGridLayout()")
        self.panel = QWidget()
        self.redditWidget = QWidget()

        '''redditSubs Layout -----------------------------------------------------'''

        lay1 = QVBoxLayout()
        self.redditWidget.setLayout(lay1)

        subscribedList = ['AccidentalRenaissance', 'HadToHurt', 'WidescreenWallpaper', 'iamverybadass',
                          'LivestreamFail', 'CatastrophicFailure', 'KidsAreFuckingStupid', 'therewasanattempt',
                          'BetterEveryLoop', 'madlads', 'IdiotsInCars', 'youseeingthisshit', 'blackmagicfuckery',
                          'WatchPeopleDieInside', 'SequelMemes', 'iamatotalpieceofshit', 'cleavesdropping',
                          'PewdiepieSubmissions', 'technicallythetruth', 'KeanuBeingAwesome', 'science', 'TikTokCringe',
                          'Cringetopia']



        for i in range(len(subscribedList)):
            lay1.addWidget(QLabel('/r' + subscribedList[i]))
            favIcon = QPushButton('Favs')
            favIcon.setFixedSize(40, 40)
            lay1.addWidget(favIcon, alignment=Qt.AlignRight)
        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setWidget(self.redditWidget)
        lay1.addWidget(scroll) ##
        self.redditWidget.setObjectName('backgroundColor')
        self.redditWidget.setStyleSheet('QLabel {color: #707070; font-size: 15px;}'
                                        'QWidget#backgroundColor {background-color: rgb(20, 21, 24)};'
                                        'QPushButton {text-align:center; font-size: 15px;}')

        self.redditSubs.addWidget(self.redditWidget)

        '''------------------------------------------------------'''

        """AddWidgets"""

        self.picLayout.addWidget(self.display)
        self.downloadBar.addWidget(self.download)
        self.profileInfo.addWidget(self.panel)
        lay = QVBoxLayout(self.panel)
        lay.addWidget(self.fileInfo)

        """Stylesheet"""
        self.panel.setStyleSheet("background-color: red;")

        """Initiating  mainLayout """
        self.window = QWidget()
        self.window.setLayout(self.mainLayout)
        self.setCentralWidget(self.window)


if __name__ == "__main__":
    app = QApplication([])
    w = Window()
    w.showMaximized()
    app.exec_()

बटन लेबल के समान स्तर पर नहीं हैं, और स्क्रॉल बार काम नहीं कर रहा है और दिखाई दे रहा है। '##' कमेंट करने पर ही चलता है।

0
CURVX 1 पद 2019, 22:27

1 उत्तर

सबसे बढ़िया उत्तर
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QGridLayout, QWidget, QVBoxLayout, QHBoxLayout
from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QPushButton, QScrollArea


class Window(QMainWindow):
    def __init__(self, parent=None):
        super(Window, self).__init__(parent)
        self.setWindowTitle("MCVE")

        """Window Background"""
        self.setAutoFillBackground(True)
        p = self.palette()
        p.setColor(self.backgroundRole(), Qt.darkYellow)
        self.setPalette(p)

        """Core Layouts"""
        self.mainLayout = QGridLayout()
        self.picLayout = QHBoxLayout()
        self.redditSubs = QVBoxLayout()
        self.downloadBar = QHBoxLayout()
        self.profileInfo = QGridLayout()

        """Nested Layout"""
        self.mainLayout.addLayout(self.profileInfo, 0, 0)
        self.mainLayout.addLayout(self.picLayout, 0, 1)
        self.mainLayout.addLayout(self.redditSubs, 1, 0)
        self.mainLayout.addLayout(self.downloadBar, 1, 1)

        """Widgets"""
        self.display = QLabel("QHBoxLayout()")
        self.download = QLabel("QHBoxLayout()")
        self.subs = QLabel("QVBoxLayout()")
        self.fileInfo = QLabel("QGridLayout()")
        self.panel = QWidget()
        self.redditWidget = QWidget()

        '''redditSubs Layout -----------------------------------------------------'''

        lay1 = QGridLayout()
        self.redditWidget.setLayout(lay1)

        subscribedList = ['AccidentalRenaissance', 'HadToHurt', 'WidescreenWallpaper', 'iamverybadass',
                          'LivestreamFail', 'CatastrophicFailure', 'KidsAreFuckingStupid', 'therewasanattempt',
                          'BetterEveryLoop', 'madlads', 'IdiotsInCars', 'youseeingthisshit', 'blackmagicfuckery',
                          'WatchPeopleDieInside', 'SequelMemes', 'iamatotalpieceofshit', 'cleavesdropping',
                          'PewdiepieSubmissions', 'technicallythetruth', 'KeanuBeingAwesome', 'science', 'TikTokCringe',
                          'Cringetopia']



        for i in range(len(subscribedList)):
            lay1.addWidget(QLabel('/r' + subscribedList[i]),i,0)
            favIcon = QPushButton('Favs')
            favIcon.setFixedSize(40, 40)
            lay1.addWidget(favIcon, i,1)
        scroll = QScrollArea()
        scroll.setWidgetResizable(True)
        scroll.setWidget(self.redditWidget)
        # lay1.addWidget(scroll)
        self.redditWidget.setObjectName('backgroundColor')
        self.redditWidget.setStyleSheet('QLabel {color: #707070; font-size: 15px;}'
                                        'QWidget#backgroundColor {background-color: rgb(20, 21, 24)};'
                                        'QPushButton {text-align:center; font-size: 15px;}')

        self.redditSubs.addWidget(scroll)

        '''------------------------------------------------------'''

        """AddWidgets"""

        self.picLayout.addWidget(self.display)
        self.downloadBar.addWidget(self.download)
        self.profileInfo.addWidget(self.panel)
        lay = QVBoxLayout(self.panel)
        lay.addWidget(self.fileInfo)

        """Stylesheet"""
        self.panel.setStyleSheet("background-color: red;")

        """Initiating  mainLayout """
        self.window = QWidget()
        self.window.setLayout(self.mainLayout)
        self.setCentralWidget(self.window)


if __name__ == "__main__":
    app = QApplication([])
    w = Window()
    w.showMaximized()
    app.exec_()

जैसा मैं चाहता था वैसा ही।

0
CURVX 2 पद 2019, 23:01