PyQt5:在GridLayout中添加一定数量的按钮后,程序崩溃

2024-09-26 18:06:45 发布

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

我正在做一个按钮网格。我意识到如果我用行创建一个网格>;80和cols>;90,然后我的程序崩溃了。我所说的“崩溃”是指程序窗口打开一秒钟,然后再次关闭。更奇怪的是,我在命令提示符中没有收到任何错误消息

有人知道为什么会这样吗

self.midColLayout = QVBoxLayout()
self.graphWidget = QWidget(self)
self.graphWidget.setStyleSheet("background-color: white; padding:0px;")
self.graphWidget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

self.vLayout = QVBoxLayout()
self.hLayout = QHBoxLayout()

self.gridLayout = QGridLayout()
self.gridLayout.setSpacing(0) 
self.hLayout.addLayout(self.gridLayout)
self.vLayout.addLayout(self.hLayout)

self.buttons = []
for x in range(0, 80): # If I put 81, then the program crashes
    l = []
    for y in range(0, 90): # If I put 91, then the program crashes
        self.button = QPushButton()
        l.append(self.button)
        self.gridLayout.addWidget(self.button, x, y)
    self.buttons.append(l)

self.graphWidget.setLayout(self.vLayout)
self.midColLayout.addWidget(self.graphWidget)


Tags: gtself程序网格buttonbuttonsignoredvlayout
1条回答
网友
1楼 · 发布于 2024-09-26 18:06:45

试试看

import sys
from PyQt5.Qt import *


class Widget(QWidget):
    def __init__(self):
        super().__init__()

        self.midColLayout = QVBoxLayout()
        self.graphWidget = QWidget(self)                                         ## self
        self.graphWidget.setStyleSheet("background-color: white; padding:0px;")
        self.graphWidget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

        self.vLayout = QVBoxLayout()
        self.hLayout = QHBoxLayout()

        self.gridLayout = QGridLayout()
        self.gridLayout.setSpacing(0) 
        self.hLayout.addLayout(self.gridLayout)
        self.vLayout.addLayout(self.hLayout)

        self.buttons = []
        for x in range(0, 80): # If I put 81, then the program crashes
            l = []
            for y in range(0, 90): # If I put 91, then the program crashes
                self.button = QPushButton()
                l.append(self.button)
                self.gridLayout.addWidget(self.button, x, y)
            self.buttons.append(l)

        self.graphWidget.setLayout(self.vLayout)
#?        self.midColLayout.addWidget(self.mapLabel)
        self.midColLayout.addWidget(self.graphWidget)

if __name__ == '__main__':
    myapp = QApplication(sys.argv)
    mywin = Widget()
    mywin.resize(400, 400)
    mywin.show()
    sys.exit(myapp.exec_())

enter image description here


The code works! I noticed that the grid goes outside of the window. Is there a quick fix for that?

试试看

import sys
from PyQt5.Qt import *


class Widget(QWidget):
    def __init__(self):
        super().__init__()

        self.midColLayout = QVBoxLayout()
        self.graphWidget = QWidget(self)                                          ## self
        self.graphWidget.setStyleSheet("background-color: white; padding:0px;")
        self.graphWidget.setSizePolicy(QSizePolicy.Ignored, QSizePolicy.Ignored)

        self.vLayout = QVBoxLayout(self)                                          # +++ self
        self.hLayout = QHBoxLayout()

        self.gridLayout = QGridLayout()
        self.gridLayout.setSpacing(0) 
        self.hLayout.addLayout(self.gridLayout)
        self.vLayout.addLayout(self.hLayout)

        self.buttons = []
        for x in range(0, 80): # If I put 81, then the program crashes
            l = []
            for y in range(0, 90): # If I put 91, then the program crashes
                self.button = QPushButton()
                l.append(self.button)
                self.gridLayout.addWidget(self.button, x, y)
            self.buttons.append(l)

#        self.graphWidget.setLayout(self.vLayout)                                   #  -
#?        self.midColLayout.addWidget(self.mapLabel)
        self.midColLayout.addWidget(self.graphWidget)

if __name__ == '__main__':
    myapp = QApplication(sys.argv)
    mywin = Widget()
    mywin.resize(400, 400)
    mywin.show()
    sys.exit(myapp.exec_())

enter image description here

相关问题 更多 >

    热门问题