peter_huckle | 2020-07-17 22:49:09 UTC | #1
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)
Luca | 2020-07-18 10:51:38 UTC | #2
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?
peter_huckle | 2020-07-18 17:37:21 UTC | #3
Many thanks for your reply. It came from the file dialog_2.py in pyqt5-source.zip which I downloaded about 5 days ago.
martin | 2020-07-19 10:28:00 UTC | #4
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.
class CustomDialog(QDialog): def __init__(self, parent): super().__init__(parent)
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.
peter_huckle | 2020-07-19 19:06:45 UTC | #5
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!
Luca | 2020-07-20 12:34:52 UTC | #6
Well done Peter, did you work on a concrete project or for now you're just learning something new about the qt library?
peter_huckle | 2020-07-21 04:13:12 UTC | #7
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.
Luca | 2020-07-21 15:00:26 UTC | #8
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.
Enzo_C_C_Maimone | 2021-02-07 17:10:57 UTC | #9
Hi Martin 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. Enzo
martin | 2021-02-09 11:17:04 UTC | #10
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!
To support developers in [[ countryRegion ]] I give a [[ localizedDiscount[couponCode] ]]% discount with the code [[ couponCode ]] — Enjoy!
For [[ activeDiscount.description ]] I'm giving a [[ activeDiscount.discount ]]% discount with the code [[ couponCode ]] — Enjoy!