Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. NameError: name 'self' is not defined
QtWS25 Last Chance

NameError: name 'self' is not defined

Scheduled Pinned Locked Moved Solved General and Desktop
6 Posts 5 Posters 2.0k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    john_hobbyist
    wrote on 3 Jan 2021, 17:39 last edited by
    #1

    This is the code for opening .tif on a PyQT window:

    import sys
    from PyQt5.QtWidgets import QApplication, QMainWindow
     
    app = QApplication(sys.argv)
     
    window = QMainWindow()
    window.show()
     
    def getFrame(self):
        fp = r'/home/UbuntuUser/Desktop/UAV.tif'
        self.img = cv2.imdecode(np.fromfile(fp, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
        print(self.img)
     
    self.getFrame()
     
    # Start the event loop.
    app.exec_()
    

    Why do I get this error??

    Traceback (most recent call last):
      File "mycode.py", line 14, in <module>
        self.getFrame()
    NameError: name 'self' is not defined
    
    1 Reply Last reply
    0
    • C Online
      C Online
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on 3 Jan 2021, 17:44 last edited by
      #2

      @john_hobbyist said in NameError: name 'self' is not defined:

      Why do I get this error??

      Because you try to call a function but you're not in a class so self is not available.. Python basics.

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      1 Reply Last reply
      2
      • J Offline
        J Offline
        john_hobbyist
        wrote on 3 Jan 2021, 18:23 last edited by
        #3

        Because I cannot understand what to do with the 'self' I removed it and when running the code, instead of getting an image, I get a white image. Is this due to the 'self', or it is because of the rest of the code?

        K 1 Reply Last reply 8 Jan 2021, 11:10
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 3 Jan 2021, 19:28 last edited by
          #4

          Hi,

          From your code, at no point are you showing any image on anything.

          You are loading an image mixing numpy and OpenCV.

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          1
          • H Offline
            H Offline
            hachbani
            wrote on 8 Jan 2021, 11:08 last edited by hachbani 1 Aug 2021, 11:08
            #5

            Not here for a solution to your problem, but I advise you to watch this video

            1 Reply Last reply
            1
            • J john_hobbyist
              3 Jan 2021, 18:23

              Because I cannot understand what to do with the 'self' I removed it and when running the code, instead of getting an image, I get a white image. Is this due to the 'self', or it is because of the rest of the code?

              K Offline
              K Offline
              KroMignon
              wrote on 8 Jan 2021, 11:10 last edited by
              #6

              @john_hobbyist said in NameError: name 'self' is not defined:

              Because I cannot understand what to do with the 'self' I removed it

              I would suggest you to learn some Python basics, like class ==> https://www.geeksforgeeks.org/self-in-python-class/

              It is an old maxim of mine that when you have excluded the impossible, whatever remains, however improbable, must be the truth. (Sherlock Holmes)

              1 Reply Last reply
              1

              1/6

              3 Jan 2021, 17:39

              • Login

              • Login or register to search.
              1 out of 6
              • First post
                1/6
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved