AttributeError: type object 'Qt' has no attribute 'Alignment'

Heads up! You've already completed this tutorial.

AstroLinux | 2021-05-13 22:25:55 UTC | #1

Hello, I get the error during run the code from Listing 13. basic/widgets_1.py (page 38, "Create GUI Applications with Python & Qt6" The hands-on guide to making apps with Python Version 1.0, 2021-04-09).

python
Traceback (most recent call last):
  File "/home/oleh/PycharmProjects/pythonProject5/main.py", line 24, in <module>
    window = MainWindow()
  File "/home/oleh/PycharmProjects/pythonProject5/main.py", line 17, in __init__
    widget.setAlignment( Qt.Alignment.AlignHCenter | Qt.Alignment.AlignVCenter)  # <2>
AttributeError: type object 'Qt' has no attribute 'Alignment'

PedanticHacker | 2021-05-13 22:49:03 UTC | #2

Hello, @AstroLinux, welcome to the forum!

Try changing Qt.Alignment to Qt.AlignmentFlag and it should work.


martin | 2021-05-14 16:15:56 UTC | #3

@AstroLinux this is due to a change in PyQt 6.1 (released 11th May) -- the enum names were updated to bring it in line with PySide6.

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt6
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]

It's a good change, as it keeps PyQt/PySide more compatible, but it does mean I need to re-do all the code examples in the book! I'll be releasing an update shortly to fix this.

edit: @AstroLinux @PedanticHacker the updated version of the PyQt6 (for 6.1) has been uploaded now. You can download it from https://account.mfitzp.com (once logged in).

There weren't that many changes in the end, still I may have missed some -- let me know if you spot anything amiss. I wanted to get this out quickly as I've already have 5 emails about it this morning :D


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

AttributeError: type object 'Qt' has no attribute 'Alignment' was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.