When I run the file dialogs_2.py I get the output as follows when I click the button:
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:
def init(self, arg)
Thanks for the information.
From the tutorial I see that the constructor of the class CustomDialog is already implemented to expect parameters:
def __init__(self, *args, **kwargs):
super(CustomDialog, self).__init__(*args, **kwargs)
Could you specify where does your code come from?
Many thanks for your reply. It came from the file dialog_2.py in pyqt5-source.zip which I downloaded about 5 days ago.
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
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.
def __init__(self, parent):
def __init__(self, parent):
…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.
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!
Well done Peter, did you work on a concrete project or for now you’re just learning something new about the qt library?
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.
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.
Both snippets you suggest above work ok, exactly with the behavior you mention when we click the main window.
The code in the HTML page, though, is not reflecting those snippets. I was following the tutorial copying the code directly, and when I run it, I got the “init() takes 1 positional argument, but 2 were given” message.
Luckily this thread was available at the bottom of the page.
Thanks for the heads up @Enzo_C_C_Maimone I’ve updated the code on that page (I’m in the process of updating all the examples to sync better with the latest edition of the book). It now also doesn’t depend on the earlier tutorial, has a working “base” example and explains the use of parent correctly.
Take a look & let me know what you think!