I am creating a window that allows me to add a array of text boxes when I press a button, but when, I press the button, and call the function, I get an error, which says 'IndicSelectWindow' object has no attribute "whatever ". I Try something else easier like changing what a label said, but I can not access the label either. How could I do to fix it? please... :(
import sys
from PyQt4.QtGui import *
class IndicSelectWindow(QDialog):
def __init__(self, parent=None):
super(IndicSelectWindow, self).__init__(parent=parent)
# cero un layout vertical para las matrices
layoutmatrices = QVBoxLayout()
##########################
# create the area for the matrix
scrollarea = QScrollArea()
scrollarea.setWidgetResizable(True)
widgetdelscrollarea = QWidget()
layoutgrilla = QGridLayout(widgetdelscrollarea)
scrollarea.setWidget(widgetdelscrollarea)
###########################
# now now place elements in the array layout
#the elements consist of a layout where the buttons are placed and another layout where the area is placed
layoutelementosmatriz1 = QHBoxLayout()
labelm1 = QLabel("Matriz 1")
labelmf1 = QLabel("Filas")
labelmc1 = QLabel("Columnas")
botonm1 = QPushButton("Aceptar")
text_m1f = QLineEdit()
text_m1c = QLineEdit()
layoutelementosmatriz1.addWidget(labelm1)
layoutelementosmatriz1.addWidget(labelmf1)
layoutelementosmatriz1.addWidget(text_m1f)
layoutelementosmatriz1.addWidget(labelmc1)
layoutelementosmatriz1.addWidget(text_m1c)
layoutelementosmatriz1.addWidget(botonm1)
layoutelementosmatriz1.setSpacing(20)
botonm1.clicked.connect(lambda: self.create_matrix()) ##eventos
layoutmatrices.addLayout(layoutelementosmatriz1)
layoutmatrices.addWidget(scrollarea)
####################################3 i create the layout for the frame
layoutgeneral = QHBoxLayout()
self.setLayout(layoutgeneral)
self.resize(800, 500)
#################################
# I add the elements to the frame
layoutgeneral.addLayout(layoutmatrices)
###################################
def create_matrix(self): #this method I use to fill the array with "QLineEdit"
self.labelm1.setText("gdsgs")
for i in range(10):
for j in range(10):
self.layoutgrilla.addWidget(QLineEdit(), i, j) #line 18
if __name__ == '__main__':
app = QApplication(sys.argv)
w = IndicSelectWindow()
w.show()
sys.exit(app.exec_())
The problem is caused because the variables can only be accessed in the context they are created, in the case of your code the constructor. Only the members of the classes are accessible in the whole class for it we must put self to the name of the variable, for example:
layoutgrilla
to
self.layoutgrilla