2

I have reviewed QGIS 2.18 plugin to port in QGIS 3.x version, but when I run that QGIS 3.x plugin I get this error message:

  File "C:/Users/kapil/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\polygon_divider\Polygon_Divider.py", line 1331
    layers = [tree_layer.layer()
                                ^
TabError: inconsistent use of tabs and spaces in indentation

So, how to solve or fix that error?

My code link is below: https://github.com/kapildevadk/Polygon-Divider/blob/master/Polygon_Divider.py Error lines are:

def run(self):

"""

* Run method that performs all the real work

"""

# Fetch the currently loaded layers

layers = [tree_layer.layer() 

for tree_layer in QgsProject.instance().layerTreeRoot().findLayers()]

And extra lines are;

def unload(self):

    """Removes the plugin menu item and icon from QGIS GUI."""

    for action in self.actions:

        self.iface.removePluginMenu(

            self.tr(u'&Polygon Divider'),

            action)

        self.iface.removeToolBarIcon(action)

    # remove the toolbar

    del self.toolbar





def select_output_file(self):

    """

    * JJH: Open file browser

    """



    # get filename from dialog  

    filename = QFileDialog.getSaveFileName(self.dlg, "Select output file ","", '*.shp')



    # verify that a name was selected

    if filename != "":



        # clear previous value

        self.dlg.lineEdit_2.clear()



        # make sure that an extension was included

        if filename[-4:] != '.shp':

            filename += '.shp'



        # put the result in the text box on the dialog

        self.dlg.lineEdit_2.setText(filename)





def startWorker(self, inFile, outFilePath, targetArea, absorbFlag, direction):

    """

    * JJH: Run the polygon division in a thread, feed back to progress bar

    """



    worker = ExampleWorker(inFile, outFilePath, targetArea, absorbFlag, direction)

    start_worker(worker, self.iface, 'running the worker')





def run(self):

    """

    * Run method that performs all the real work

    """

    # Fetch the currently loaded layers

    layers = [tree_layer.layer() 

    for tree_layer in QgsProject.instance().layerTreeRoot().findLayers()]

    # Clear the contents of the comboBox and lineEdit from previous runs

    self.dlg.comboBox.clear()

    self.dlg.lineEdit.clear()





    # Populate the comboBox with names of all the loaded layers

    self.dlg.comboBox.addItems([layer.name() for layer in layers])



    # populate comboBox_2 with the possible directions

    self.dlg.comboBox_2.clear() # need to clear here or it will add them all again every time the dialog is opened

    self.dlg.comboBox_2.addItems(['left to right', 'right to left', 'bottom to top', 'top to bottom'])



    # Populate the comboBox with names of all the loaded layers

    self.dlg.comboBox_2.addItems([layer.name() for layer in layers])



    # show the dialog

    self.dlg.show()



    # Run the dialog event loop

    result = self.dlg.exec_()
8
  • Please place the relevant piece of code in the question itself, though this seems less a GIS issue that pure Python syntax. – Vince Feb 1 '20 at 16:50
  • The code I have placed has much more line than this site accept. – Kapil Dev Adhikari Feb 1 '20 at 16:56
  • The ten lines above the failure should suffice. – Vince Feb 1 '20 at 16:58
  • 2
    Also you might also try combining the list comprehension that's currently split between lines 1331 and 1332 into one line: layers = [tree_layer.layer() for tree_layer in QgsProject.instance().layerTreeRoot().findLayers()] – Nick Feb 1 '20 at 17:15
  • 1
    Indeed, @Nick seems to have nailed it. There is no paren involved, so a line continuation backslash is necessary (or you can place it on line for whic it continues). As for indentation, sometimes you need to retype a line to remove an unseen escape sequence. – Vince Feb 1 '20 at 17:28

Browse other questions tagged or ask your own question.