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.

The complete guide to packaging Python GUI applications with PyInstaller.
[[ 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).

Create GUI Applications with Python & Qt6 by Martin Fitzpatrick — (PyQt6 Edition) The hands-on guide to making apps with Python — Over 10,000 copies sold!

More info Get the book

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


PyQt/PySide 1:1 Coaching with Martin Fitzpatrick — Get one on one help with your Python GUI projects. Working together with you I'll identify issues and suggest fixes, from bugs and usability to architecture and maintainability.

Book Now 60 mins ($195)

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

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. Martin founded PythonGUIs to provide easy to follow GUI programming tutorials to the Python community. He has written a number of popular https://www.martinfitzpatrick.com/browse/books/ on the subject.