bryon | 2020-10-24 20:46:54 UTC | #1
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().__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.
bryon | 2020-10-25 00:56:29 UTC | #3
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
Mark_Salerno | 2020-11-29 14:04:31 UTC | #4
I an running the example compiled_example.py from the tutorial download in the designer examples: compiled_example.py
Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.
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
martin | 2020-11-30 17:48:35 UTC | #5
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_()
martin | 2020-11-30 17:49:06 UTC | #6
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.
Mark_Salerno | 2020-11-30 18:49:27 UTC | #7
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
Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PySide6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!