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.

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.

More info 60 mins ($195)

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


Packaging Python Applications with PyInstaller by Martin Fitzpatrick — This step-by-step guide walks you through packaging your own Python applications from simple examples to complete installers and signed executables.

More info Get the book

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.