0

This (part of) code give me error:

def pickFile(self):
 global imgPath01
imgPath01 = QtGui.QFileDialog.getOpenFileName(self, 'Open File', "~/", "Images (*.png *.jpg)")
print(imgPath01)
self.refreshImage()
pass

I have error on self and self.refreshImage. It says: NameError: name 'self' is not defined.

The full code can be found here.

asked Sep 20, 2017 at 9:29
2
  • That paste bin you posted has broken code Commented Sep 20, 2017 at 9:33
  • I know, already fixed it a bit. Just the errors above was remained.. Commented Sep 20, 2017 at 9:39

2 Answers 2

2

In Python, indentation matters. Those lines that aren't indented aren't part of the function, so Python tries to execute them as soon as the script is run, at which point it looks for a global variable called self, which doesn't exist.

The solution is to indent the remaining lines of the function.

Edit: Similarly, the functions you have defined aren't indented enough to be member functions of the class.

Your code should be formatted to this:

class LineDetection(QtGui.QWidget):
 # ... other functions go here ...
 # Note the indentation of the next line
 def pickFile(self):
 global imgPath01
 imgPath01 = QtGui.QFileDialog.getOpenFileName(self, 'Open File', "~/", "Images (*.png *.jpg)")
 print(imgPath01)
 self.refreshImage()
answered Sep 20, 2017 at 9:32
Sign up to request clarification or add additional context in comments.

2 Comments

Similarly, the functions you have defined aren't indented enough to be member functions of the class. This is the solution. Thanks. Can you look also at the Qt at line 206 ? Thanks again.
@Link both the indentation of the functions and the indentation of the lines within the function are problems e.g. self.refreshImage() should be indented twice as show in my edit. I don't know about the QT library, so if there's still a problem then post a separate question about that.
1

You only need to use self as a parameter when the function is part of a class, because self is the class. When it's a normal function, you can have no parameters at all, which is what you should have done.

EDIT: after looking at the full code, I suppose the functions with the self parameter are supposed to be part of the class you define at the beginning. If so, indent them. It's necessary to do it, it's not just a choice like in many other languages: Python works this way. Be very careful with the indentation, as you might have lots of errors because of that, but FYI, Python is an interpreted language, so it does not compile and show errors before executing: it executes directly and if it finds an error it will show you that one, but no more. Review all the code.

answered Sep 20, 2017 at 9:32

3 Comments

the functions with the self parameter are supposed to be part of the class you define at the beginning. If so, indent them. This is it. It has to be indented also. Didn't knew about that.
Everything needs to be indented here. If something is part of a function, it needs to be more indented than the declaration. If that function is part of a class, everything has to be more indented than the declaration of the class. Welcome to Python, remember not to make this mistake again.
I hope. Working with Python since June, indent was the first thing. Well, never stop to learn ;)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.