Help with Creating Your First App Tutorial

I’m struggling a bit with the very first lesson. I’m following along the video at the top of the page since the codeblocks on the page are different and don’t seem to be working for me (missing imports as far as I can tell). I’ve gotten to the part where we use a class to create the window but when I attempt to change the title of the window it doesn’t change. Code is below, I’m on Windows 10 with Python 3.7.4. and Python 3.9 (I just reinstalled it as a test)

import sys
from PySide2.QtWidgets import *
from PySide2.QtCore import *
from PySide2.QtGui import *

class MainWindow(QMainWindow):    

    def __init__(self, *args, **kwargs):
        super(MainWindow, self).__init__(*args,**kwargs)
        self.setWindowTitle('Awesome App')

app = QApplication(sys.argv)
window = QWidget()
window.show() # IMPORTANT! Windows are hidden by default.

app.exec_()

Setting the title with window.setWindowTitle(‘whatever’) does work though.

window = MainWindow() worked. I’m guessing this is an error in the lesson. Further down the variable is changed though to that. I didn’t notice it changes

I an running the example compiled_example.py from the tutorial download in the designer examples:
compiled_example.py

import random
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow

from MainWindow import Ui_MainWindow


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        # You can still override values from your UI file within your code,
        # but if possible, set them in Qt Creator. See the properties panel.
        f = self.label.font()
        f.setPointSize(25)
        self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.label.setFont(f)

        # Signals from UI widgets can be connected as normal.
        self.pushButton.pressed.connect(self.update_label)

    def update_label(self):
        #n = n + 1
        n = random.randint(1, 6)
        self.label.setText("%d" % n)


app = QApplication(sys.argv)
w = MainWindow()
app.exec_()

I wanted to show a number incrementing in the label and I commented out the random line an d added n=n+1 line. The program crashes when I push the button. If I leave the random line in and add the n=n+1 line it crashes when I push the button.

Can someone explain why this is happening

Mark

Hi Mark

The reason your n = n + 1 line causes the program to crash is that n is not defined in update_label – you can’t add 1 to a variable that doesn’t exist. There is a second issue in that within that method the n variable is local, which means that even if you do define it, on exiting that method n will be deleted.

The solution to this is to store your reference to n on the object, via self, e.g.

self.n = self.n + 1

But even then, you still need to define self.n first. To do this, in the __init__ block of your class, you can write

self.n = 0

…to initialize it to zero. The full code would be

import random
import sys

from PyQt5.QtCore import Qt
from PyQt5.QtWidgets import QApplication, QMainWindow

from MainWindow import Ui_MainWindow


class MainWindow(QMainWindow, Ui_MainWindow):
    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.show()

        self.n = 0

        # You can still override values from your UI file within your code,
        # but if possible, set them in Qt Creator. See the properties panel.
        f = self.label.font()
        f.setPointSize(25)
        self.label.setAlignment(Qt.AlignHCenter | Qt.AlignVCenter)
        self.label.setFont(f)

        # Signals from UI widgets can be connected as normal.
        self.pushButton.pressed.connect(self.update_label)

    def update_label(self):
        self.n = self.n + 1
        self.label.setText("%d" % self.n)


app = QApplication(sys.argv)
w = MainWindow()
app.exec_()

Thanks for the feedback @bryon I’ll take another look at that tutorial, I’m in the process of updating them all to contain the full code blocks, etc.

Thanks Martin for taking the time to so thoroughly answer my question and show me the solution.

I currently program small embedded systems in c and I am not used to the self. references.

Mark