-2

Why these two function want not work ?

class Window(QtGui.QMainWindow):

 def __init__(self):
    super(Window, self).__init__()
    self.setGeometry(500, 150, 500, 600)
    self.home()

 def home(self):

    btn_run = QtGui.QPushButton("Run", self)
    btn_run.clicked.connect(self.run)
    btn_run.resize(120, 40)
    btn_run.move(220, 540)

    self.show()

 def view_splash(arg1):
    label = QLabel("<font color=red size=10<b>" + n[arg1] + "</b></font>")
    label.setWindowFlags(Qt.SplashScreen | Qt.WindowStaysOnTopHint)
    label.show()
    QtCore.QTimer.singleShot(10000, label.hide)

 def run(self):
    for i in range(len(lines)):
        n = random.choice(words)
        view_splash(0)
        view_splash(1)
        time.sleep(600)

I have that error:

  view_splash(0)
NameError: name 'view_splash' is not defined

What I'm doing wrong ? How this should look like ?

zondo
  • 19,901
  • 8
  • 44
  • 83
Caporeira
  • 49
  • 3
  • 8

1 Answers1

1

In python, it is necessary to use self to access other methods and attributes of the same object. When you simply call view_splash, python looks for the function definition but will not look at the methods of Window. By explicitly prefixing view_splash with self., python then knows that you want the method Window.view_splash and it should work as you expect.

So for your specific code, this would require you to update your run method to be the following:

def run(self):
    for i in range(len(lines)):
        n = random.choice(words)
        self.view_splash(0)
        self.view_splash(1)
        time.sleep(600)

I'm assuming that there is additional code outside of the class which defines lines and words as global variables which Window.run can then access.

Suever
  • 64,497
  • 14
  • 82
  • 101