我在PyQt中的QGridLayout没有显示所有元素,或者显示在正确的位置

时间:2016-04-21 20:34:51

标签: python qt pyqt qt-designer

当我在Qt Designer中查看我的MainWindow时,我看到了MainWindow

但是,当我将ui文件编译成python代码时,请将匹配对象(带有&#34的QGroupBox;冠军"作为标题,在匹配历史记录中),并将其移植到以便我可以构建许多将这些内容添加到滚动区域(匹配历史记录)并运行它我得到这个BAD MainWindow

组合框在窗口底部后不久结束,因此它不会永远继续,我已在代码中确认组框的宽度和高度设置正确。我还确认匹配对象确实有10个插槽并且在构建之后包含其中的项目,问题只是它在运行时看起来不正确。出了什么问题?

这是buildMatch方法:

def buildMatch(self, summonerId, matchIndex, matchId, matchWidth, matchHeight):
    # This method takes the matchIndex and matchId as an input, builds a match object, and returns it. As a part of 
    # building each match object, this method calls calculateMatchStatistics() and calculateAverages. It might be 
    # prudent later to only call self.calculateAverages() once, rather than every time, but I'm not sure.
    # Globals: none

    # Check whether we have match data for the matchID in question. If we do, 
    # continue. If not, call getMatchDetails for the match and store that data in matchHistoryDetails.
    # Keys of the matchHistoryDetails dictionary will be unicode strings. Convert the matchId to unicode 
    # before using as a key or lookup element.
    matchId = unicode(matchId)
    knownMatchIds = self.matchHistoryDetails.keys()
    if matchId not in knownMatchIds:
        self.matchHistoryDetails[matchId] = self.getMatchDetails(summonerId, matchId)

    # Load champion name from getChampionName() and lane from matchHistoryList
    championId = self.matchHistoryList["matches"][matchIndex]["champion"]
    championName = self.getChampionName(championId)

    match = QGroupBox(championName)
    match.setFixedHeight(matchWidth)
    match.setFixedWidth(matchHeight)

    matchWidget = QWidget(match)
    matchWidget.setGeometry(QRect(10, 20, matchWidth, matchHeight))
    matchLayout = QGridLayout(matchWidget)

    statisticsBoxToMatchWidthRatio = .165
    statisticsBoxToMatchHeightRatio = .3895
    statisticsBoxWidth = int(matchWidth * statisticsBoxToMatchWidthRatio)
    statisticsBoxHeight = int(matchHeight * statisticsBoxToMatchHeightRatio)

    scoreBox = QGroupBox(matchWidget)
    scoreBox.setAlignment(Qt.AlignCenter)
    scoreBox.setTitle("score")
    scoreBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    scoreBox.setToolTip("Score, in the format of kills-deaths-assists")
    scoreValue = QLabel(scoreBox)
    scoreValue.setGeometry(QRect(10, 29, statisticsBoxWidth, statisticsBoxHeight))
    scoreValue.setAlignment(Qt.AlignCenter)
    scoreValue.setStyleSheet('font: 9pt "Calibri";')

    kdaBox = QGroupBox(matchWidget)
    kdaBox.setAlignment(Qt.AlignCenter)
    kdaBox.setTitle("KDA")
    kdaBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    kdaBox.setToolTip("Calculated by (kills+deaths)/assists")
    kdaValue = QLabel(kdaBox)
    kdaValue.setGeometry(QRect(10, 29, statisticsBoxWidth, statisticsBoxHeight))
    kdaValue.setAlignment(Qt.AlignCenter)

    killParticipationPercentBox = QGroupBox(matchWidget)
    killParticipationPercentBox.setAlignment(Qt.AlignCenter)
    killParticipationPercentBox.setTitle("Kill part. %")
    killParticipationPercentBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    killParticipationPercentBox.setToolTip("The percentage of your teams kills you contributed to")
    killParticipationPercentValue = QLabel(killParticipationPercentBox)
    killParticipationPercentValue.setGeometry(QRect(10, 29, statisticsBoxWidth, statisticsBoxHeight))
    killParticipationPercentValue.setAlignment(Qt.AlignCenter)

    goldPerMinBox = QGroupBox(matchWidget)
    goldPerMinBox.setAlignment(Qt.AlignCenter)
    goldPerMinBox.setTitle("gold/min")
    goldPerMinBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    goldPerMinBox.setToolTip("Gold earned per minute")
    goldPerMinValue = QLabel(goldPerMinBox)
    goldPerMinValue.setGeometry(QRect(11, 29, statisticsBoxWidth, statisticsBoxHeight))
    goldPerMinValue.setAlignment(Qt.AlignCenter)

    wardScoreBox = QGroupBox(matchWidget)
    wardScoreBox.setAlignment(Qt.AlignCenter)
    wardScoreBox.setTitle("ward score")
    wardScoreBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    wardScoreBox.setToolTip("Calculated by wards placed + wards killed")
    wardScoreValue = QLabel(wardScoreBox)
    wardScoreValue.setGeometry(QRect(10, 30, statisticsBoxWidth, statisticsBoxHeight))
    wardScoreValue.setAlignment(Qt.AlignCenter)

    csPerMinBox = QGroupBox(matchWidget)
    csPerMinBox.setAlignment(Qt.AlignCenter)
    csPerMinBox.setTitle("cs/min")
    csPerMinBox.setStyleSheet('QGroupBox {font: 8pt "Calibri"}')
    csPerMinBox.setToolTip("Creeps killed per minute")
    csPerMinValue = QLabel(csPerMinBox)
    csPerMinValue.setGeometry(QRect(10, 29, statisticsBoxWidth, statisticsBoxHeight))
    csPerMinValue.setAlignment(Qt.AlignCenter)

    spacerItem2 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

    laneLabel = QLabel(matchWidget)
    laneLabel.setAlignment(Qt.AlignCenter)
    laneLabel.setStyleSheet('font: 10pt "Calibri";')
    laneLabel.setToolTip("Lane")

    spacerItem3 = QSpacerItem(40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum)

    resultLabel = QLabel(matchWidget)
    resultLabel.setAlignment(Qt.AlignCenter)
    resultLabel.setStyleSheet('font: 12pt "Calibri"')
    resultLabel.setToolTip("Match result")

    matchLayout.addItem(spacerItem2, 0, 1, 1, 1)
    matchLayout.addWidget(scoreBox, 0, 2, 1, 1)
    matchLayout.addWidget(killParticipationPercentBox, 0, 3, 1, 1)
    matchLayout.addWidget(wardScoreBox, 0, 4, 1, 1)
    matchLayout.addItem(spacerItem3, 0, 5, 1, 1)
    matchLayout.addWidget(resultLabel, 0, 6, 1, 1)
    matchLayout.addWidget(laneLabel, 1, 0, 1, 1)
    matchLayout.addWidget(kdaBox, 1, 2, 1, 1)
    matchLayout.addWidget(goldPerMinBox, 1, 3, 1, 1)
    matchLayout.addWidget(csPerMinBox, 1, 4, 1, 1)

    matchLayout.setColumnStretch(0, 10)
    matchLayout.setColumnStretch(1, 3)
    matchLayout.setColumnStretch(2, 11)
    matchLayout.setColumnStretch(3, 10)
    matchLayout.setColumnStretch(4, 10)
    matchLayout.setColumnStretch(5, 3)
    matchLayout.setColumnStretch(6, 10)
    matchLayout.setRowStretch(0, 10)
    matchLayout.setRowStretch(1, 10)

    ##################################################

    # Set values for each item
    matchWon = self.matchHistoryStatistics["matches"][matchId]["won"]
    if matchWon:
        result = "Win"
    else:
        result = "Loss"
    score = self.matchHistoryStatistics["matches"][matchId]["score"]
    kda = self.matchHistoryStatistics["matches"][matchId]["kda"]
    kdaAverage = self.matchHistoryStatistics["kdaAverage"]
    killParticipationPercent = self.matchHistoryStatistics["matches"][matchId]["killParticipationPercent"]
    killParticipationPercentAverage = self.matchHistoryStatistics["killParticipationPercentAverage"]
    wardScore = self.matchHistoryStatistics["matches"][matchId]["wardScore"]
    wardScoreAverage = self.matchHistoryStatistics["wardScoreAverage"]
    goldPerMin = self.matchHistoryStatistics["matches"][matchId]["goldPerMin"]
    goldPerMinAverage = self.matchHistoryStatistics["goldPerMinAverage"]
    csPerMin = self.matchHistoryStatistics["matches"][matchId]["csPerMin"]
    csPerMinAverage = self.matchHistoryStatistics["csPerMinAverage"]
    lane = self.matchHistoryList["matches"][matchIndex]["lane"].lower().capitalize()
    if lane == "Bottom":
        lane = "Bot"

    resultLabel.setText(result)
    scoreValue.setText(str(score))
    kdaValue.setText(str(kda))
    killParticipationPercentValue.setText(str(killParticipationPercent))
    wardScoreValue.setText(str(wardScore))
    goldPerMinValue.setText(str(goldPerMin))
    csPerMinValue.setText(str(csPerMin))
    laneLabel.setText(lane)

    if result == "Win":
        resultLabel.setStyleSheet('color: green; font: 12pt "Calibri";')
    else:
        resultLabel.setStyleSheet('color: red; font: 12pt "Calibri";')
    if kda > kdaAverage:
        kdaValue.setStyleSheet('color: green; font: 9pt "Calibri";')
    else:
        kdaValue.setStyleSheet('color: red; font: 9pt "Calibri";')
    if killParticipationPercent > killParticipationPercentAverage:
        killParticipationPercentValue.setStyleSheet('color: green; font: 9pt "Calibri";')
    else:
        killParticipationPercentValue.setStyleSheet('color: red; font: 9pt "Calibri";')
    if wardScore > wardScoreAverage:
        wardScoreValue.setStyleSheet('color: green; font: 9pt "Calibri";')
    else:
        wardScoreValue.setStyleSheet('color: red; font: 9pt "Calibri";')
    if goldPerMin > goldPerMinAverage:
        goldPerMinValue.setStyleSheet('color: green; font: 9pt "Calibri";')
    else:
        goldPerMinValue.setStyleSheet('color: red; font: 9pt "Calibri";')
    if csPerMin > csPerMinAverage:
        csPerMinValue.setStyleSheet('color: green; font: 9pt "Calibri";')
    else:
        csPerMinValue.setStyleSheet('color: red; font: 9pt "Calibri";')

    return match

0 个答案:

没有答案