Dialogs_2.py error when clicking button

When I run the file dialogs_2.py I get the output as follows when I click the button:

click False
Traceback (most recent call last):
File “dialogs_2.py”, line 52, in button_clicked
dlg = CustomDialog(self)
TypeError: init() takes 1 positional argument but 2 were given
/tmp/geany_run_script_RMW6N0.sh: line 7: 120311 Aborted (core dumped) /usr/bin/python “dialogs_2.py”

I found a fix by changing this line in CustomDialog to include the arg parameter:

class CustomDialog(QDialog):
def init(self, arg)

Hi Peter,
Thanks for the information.
From the tutorial I see that the constructor of the class CustomDialog is already implemented to expect parameters:

class CustomDialog(QDialog):

    def __init__(self, *args, **kwargs):
        super(CustomDialog, self).__init__(*args, **kwargs)

Could you specify where does your code come from?

Hi Luca

Many thanks for your reply. It came from the file dialog_2.py in pyqt5-source.zip which I downloaded about 5 days ago.

Kind regards

Peter

Hi @peter_huckle welcome to the forum & thanks for letting me know. I’ll upload a fixed version of the source code shortly.

The parameter that is being passed when creating CustomDialog is self which in this case refers to the MainWindow. This is intended to set parent as the mainwindow for the dialog (so it is a dialog of that window specifically). For this to work you also need to pass the parent value up to the super class, e.g.

class CustomDialog(QDialog):
    def __init__(self, parent):
        super().__init__(parent)

Try also

class CustomDialog(QDialog):
    def __init__(self, parent):
        super().__init__()

…which just discards the parent, to see the difference in the behaviour. It’s very subtle. In both cases the active dialog will prevent interaction with the main window, but if the mainwindow is the parent of the dialog (i.e. you have passed it up to the super init), clicking on the window will flash the dialog to draw attention to it.

I’ll add a mention about this to the book as it’s a useful thing to know.

Thanks Martin

I’ve just realised that unless the arg parameter is included in the super().init(arg) statement then the button_clicked method is not triggered so that seems to be the full solution.

Plenty of time to work on this stuff here during the COVID infection. I’m on Rarotonga with no tourists to keep me busy!

Peter

Well done Peter, did you work on a concrete project or for now you’re just learning something new about the qt library?

Hi Luca

I’m wondering if I can develop an app that can help our accommodation businesses on the island get better info to manage their business. Not sure how it will develop yet, but I know that a good UI is essential as is being able to harvest data from the web.

Hi Peter,

Interesting I guess that if you came out with this idea there will be many ways to improve the management of the business and this moment could be a good moment to think about it and try to do something.

For sure the choices related to the UI will be really important for the usability of the application. There are many ways to do the same things, each one has some pros and cons depending from the requirements, from the libraries and the languages that you know, and from the time that you have.

If it wasn’t already in your plans I suggest you to design the application in a way to decouple the business logic from the UI, so that in the future if you’ll realize that the UI could have been implemented in another way or if you’ll want to extend it to work in a browser or something else, the code related to the business logic will remain unchanged and you’ll just need to focus on the new UI implementation.