python - PyQt5:将一定数量的按钮添加到 GridLayout 后程序崩溃
问题描述
我正在制作一个按钮网格。我意识到,如果我创建一个行数 > 80 且列数 > 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)
解决方案
试试看
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_())
代码有效!我注意到网格超出了窗口。有没有快速解决方法?
试试看
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_())
推荐阅读
- mysql - MySQLdb.constants.CR' 没有属性 'SECURE_AUTH
- laravel - 如何安装 Laravel Passport 且没有错误,如下所示
- python - 向后逐步选择以 AUC 为标准选择预测变量的最佳子集
- python - Jupyter Notebook 中的主题
- huffman-code - 在霍夫曼代码(具有不同长度的最佳代码)中,预期的零数是预期长度的一半
- javascript - “不等于值或类型”,无法正常工作:Javascript
- java - 令牌“:”上的语法错误,eclipse ide中的无效AssignmentOperator
- java - 尝试打开文本文件时,Java 中的 Android 应用程序崩溃
- python - Pytest-Django 客户端用户登录
- python-3.x - 为列表中的每个对象添加值