Not seeing error; experienced eyes needed

Rich Shepard rshepard at appl-ecosys.com
Thu Apr 22 00:30:44 BST 2021


I'm learning PyQt5 building my first application using it. When I run the
attached test.py code in Python3-3.7.2 it returns the following traceback:
$ python test.py 
QWidget::setLayout: Attempting to set QLayout "" on TestWindow "", which already has a layout
Traceback (most recent call last):
   File "test.py", line 50, in <module>
     bio = TestWindow()
   File "test.py", line 33, in __init__
     clicked=self.save
AttributeError: 'TestWindow' object has no attribute 'save'

I'm following Alan Moore's book, "Mastering GUI Programming with Python" and
assume my error is not in the button widget code.

A pointer to what I've done incorrectly will be greatly appreciated.

Thanks in advance,

Rich
-------------- next part --------------
#!/usr/bin/env python3

import sys

from PyQt5 import QtWidgets as qtw
from PyQt5 import QtGui as qtg
from PyQt5 import QtCore as qtc

class TestWindow(qtw.QWidget):
    def __init__(self):
        super().__init__()
        bio = qtw.QWidget(windowTitle='Test')

        # Methods
        def save(self):
            pass

        def cancel(self):
            pass
                
        # Layout
        self.group1 = qtw.QHBoxLayout()
        self.setLayout(self.group1)

        #self.row1 = qtw.QVBoxLayout()
        #self.setLayout(self.row1)

        self.button_row = qtw.QVBoxLayout()
        self.setLayout(self.button_row)
        
        self.savebutton = qtw.QPushButton(
            'Save',
            clicked=self.save
        )
        self.cancelbutton = qtw.QPushButton(
            'Cancel',
            clicked=self.cancel
        )

        # Place widgets on layouts
        
        self.button_row.addWidget(self.savebutton)
        self.button_row.addWidget(self.cancelbutton)
        self.group1.addWidget(self.button_row)
        
        self.show()

if __name__ == '__main__':
    app = qtw.QApplication(sys.argv)
    bio = TestWindow()
    sys.exit(app.exec())       


More information about the PyQt mailing list