Qt 没有属性“AlignCenter" [英] Qt has no attribute 'AlignCenter'

查看:77
本文介绍了Qt 没有属性“AlignCenter"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Python 3.5 上使用 PyQt5.

我想制作一个带有居中文本的 QLabel 小部件.因此,我使用 AlignCenter 标志调用 setAlignment 方法.

这是一个 MWE:

导入系统从 PyQt5 导入 QtWidgets,Qtapp = QtWidgets.QApplication(sys.argv)标签 = QtWidgets.QLabel()label.setAlignment(Qt.AlignCenter)

但是,我收到以下错误:

<块引用>

label.setAlignment(Qt.AlignCenter)

AttributeError: module 'PyQt5.Qt' 没有属性 'AlignCenter'

但是 Qt.AlignCenter 以及其他对齐标志在

注释掉对齐:

导入系统从 PyQt5 导入 QtWidgets、QtCoreapp = QtWidgets.QApplication(sys.argv)标签 = QtWidgets.QLabel()label.setGeometry(100, 100, 200, 100)label.setText("Hello world!")# label.setAlignment(QtCore.Qt.AlignCenter)标签.show()退出(app.exec_())

I am using PyQt5 on Python 3.5.

I want to make a QLabel widget with a centered text. Therefore, I call the setAlignment method with the AlignCenter flag.

Here is an MWE:

import sys
from PyQt5 import QtWidgets, Qt

app = QtWidgets.QApplication(sys.argv)

label = QtWidgets.QLabel()
label.setAlignment(Qt.AlignCenter)

However, I get the following error:

label.setAlignment(Qt.AlignCenter)

AttributeError: module 'PyQt5.Qt' has no attribute 'AlignCenter'

But the Qt.AlignCenter, as well as other alignment flags, are referenced in PyQt's documentation, as well as Qt's documentation.

What am I doing wrong?

解决方案

The AttributeError that is raised tells that PyQt5.Qt has no attribute called AlignCenter.

This can easily be confirmed in Python's interactive help:

>>> from PyQt5 import Qt
>>> help(Qt)

help will display a bunch of methods, but a quick search of "alignment" will give zero results.

As a matter of fact, the AlignCenter flag does not belong to the PyQt5.Qt module, but to the PyQt5.QtCore.Qt class.

Therefore, changing

label.setAlignment(Qt.AlignCenter)

into

label.setAlignment(QtCore.Qt.AlignCenter)

along with the right import will do the work.


The following code shows that this actually works. I had to add some details to the original code to make the centering visible.

import sys
from PyQt5 import QtWidgets, QtCore

app = QtWidgets.QApplication(sys.argv)

label = QtWidgets.QLabel()
label.setGeometry(100, 100, 200, 100)
label.setText("Hello world!")
label.setAlignment(QtCore.Qt.AlignCenter)

label.show()

exit(app.exec_())

With the alignment commented out:

import sys
from PyQt5 import QtWidgets, QtCore

app = QtWidgets.QApplication(sys.argv)

label = QtWidgets.QLabel()
label.setGeometry(100, 100, 200, 100)
label.setText("Hello world!")
# label.setAlignment(QtCore.Qt.AlignCenter)

label.show()

exit(app.exec_())

这篇关于Qt 没有属性“AlignCenter"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆