如何从 Qt::namespase (Qt5, Python3.x) 导入? [英] How to import from Qt:: namespase (Qt5, Python3.x)?

查看:49
本文介绍了如何从 Qt::namespase (Qt5, Python3.x) 导入?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于我的应用程序,我需要设置一些小部件参数,如对齐 (Qt::AlignBottom) 等.但我无法导入它们(其他 PyQt5 内容导入没有任何问题).

For my application, I need to set some widget parameters like alignment (Qt::AlignBottom) and others. But I can't import them (other PyQt5 stuff imports without any issues).

使用此代码

from PyQt5 import Qt

progressBar = QProgressBar(splash)
progressBar.setAlignment(Qt.AlignBottom)

我收到以下错误:

Traceback (most recent call last):
  File "run_app.py", line 50, in <module>
    runSemApp(sys.argv)
  File "run_app.py", line 32, in runSemApp
    progressBar.setAlignment(Qt.AlignBottom)
AttributeError: 'module' object has no attribute 'AlignBottom'

使用这个有效:

from PyQt5.Qt import *

progressBar = QProgressBar(splash)
progressBar.setAlignment(Qt.AlignBottom)

虽然我有一个可行的解决方案,但我只想导入 Qt.AlignBottom 而不是 *.另外,为什么 Qt.AlignBottom 不能与 from PyQt5 import Qt 一起使用?

Though I have a working solution, I would like to import only Qt.AlignBottom and not *. Also, why doesn't Qt.AlignBottom work with from PyQt5 import Qt?

推荐答案

我认为这里的困惑在于 PyQt 有一个名为 Qt 的特殊虚拟模块,它导入 一切到单个命名空间中.这是一个非常有用的功能,但很遗憾没有避免与 QtCore.Qt 的名称冲突.

I think the confusion here is that PyQt has a special virtual module called Qt, which imports everything into a single namespace. This is a quite useful feature, but it's a real shame that the name clash with QtCore.Qt wasn't avoided.

在第一个示例中,可以使用看起来有些奇怪的 Qt.Qt.AlignBottom 来修复"错误.但显然,从 QtCore 显式导入是一个更好的解决方案.还值得注意的是 PyQt5 包是一个惰性加载器,因此 import PyQt5 只会导入一个空命名空间,而无法访问其他模块.

In the first example, the error can be "fixed" by using the somewhat weird-looking Qt.Qt.AlignBottom. But obviously, explicitly importing from QtCore is a much better solution. It's also worth noting that the PyQt5 package is a lazy loader, so import PyQt5 will just import an empty namespace with no access to the other modules.

这篇关于如何从 Qt::namespase (Qt5, Python3.x) 导入?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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