简体   繁体   English

如何从Qt :: namesase(Qt5,Python3.x)导入?

[英]How to import from Qt:: namespase (Qt5, Python3.x)?

For my application, I need to set some widget parameters like alignment ( Qt::AlignBottom ) and others. 对于我的应用程序,我需要设置一些小部件参数,例如alignment( Qt::AlignBottom )等。 But I can't import them (other PyQt5 stuff imports without any issues). 但是我无法导入它们(其他PyQt5素材导入没有任何问题)。

Using this code 使用此代码

from PyQt5 import Qt

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

I got the following error: 我收到以下错误:

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'

And using this one works: 并使用此作品:

from PyQt5.Qt import *

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

Though I have a working solution, I would like to import only Qt.AlignBottom and not * . 尽管我有一个Qt.AlignBottom解决方案,但我Qt.AlignBottom导入Qt.AlignBottom而不是* Also, why doesn't Qt.AlignBottom work with from PyQt5 import Qt ? 另外,为什么Qt.AlignBottom无法与from PyQt5 import Qt

I think the confusion here is that PyQt has a special virtual module called Qt , which imports everything into a single namespace. 我认为这里的困惑在于PyQt有一个称为Qt的特殊虚拟模块,该模块将所有内容导入单个名称空间。 This is a quite useful feature, but it's a real shame that the name clash with QtCore.Qt wasn't avoided. 这是一个非常有用的功能,但QtCore.Qt是,没有避免与QtCore.Qt发生名称冲突。

In the first example, the error can be "fixed" by using the somewhat weird-looking Qt.Qt.AlignBottom . 在第一个示例中,可以使用看起来有些奇怪的Qt.Qt.AlignBottom来“修复”错误。 But obviously, explicitly importing from QtCore is a much better solution. 但是显然,从QtCore显式导入是一个更好的解决方案。 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. 还值得注意的是PyQt5包是一个惰性加载器,因此import PyQt5只会导入一个空的名称空间,而不能访问其他模块。

You can do this 你可以这样做

>>> from PyQt5.QtCore import Qt
>>> Qt.AlignBottom
64
>>>

You can't import AlignBottom only because QtCore is not a package itself, it's just a module on it's own (a single file). 您不能仅因为QtCore本身不是包,而仅是一个模块(单个文件)来导入AlignBottom it's important to know that all packages are modules, but not all modules are packages 重要的是要知道所有软件包都是模块,但并非所有模块都是软件包

so this won't work 所以这行不通

import PyQt5.QtCore.Qt

ImportError: No module named 'PyQt5.QtCore.Qt'; 'PyQt5.QtCore' is not a package
>>> import PyQt5.QtCore
>>> QtCore
<module 'PyQt5.QtCore' from '/usr/lib/python3.5/site-packages/PyQt5/QtCore.so'>
>>> import PyQt5
>>> PyQt5
<module 'PyQt5' from '/usr/lib/python3.5/site-packages/PyQt5/__init__.py'>
>>>

Looking at the output you can see that QtCore is a single file which contains a class Qt that contains other classes and methods on which AlignBottom is part of, you can see that with. 查看输出,您可以看到QtCore是单个文件,其中包含类Qt ,该类包含AlignBottom所在的其他类和方法,您可以看到。

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

On the otherhand PyQt5 is a package (folder containing other modules) points to it's __init__.py 另一方面,PyQt5是一个包(包含其他模块的文件夹),指向它的__init__.py

i'll suggest you read the docs on Modules and this SO question 我会建议你阅读文档上的模块,这SO问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM