简体   繁体   English

导入PyQt4不包括PyQt4.QtCore或QtGui

[英]import PyQt4 does not include PyQt4.QtCore or QtGui

Why do neither of these statements import QtCore, QtGui, QtNetwork or any of the others? 为什么这些语句都不导入QtCore,QtGui,QtNetwork或其他任何语句? I've searched so long and can't find anyone to answer such a simple question. 我搜索了很长时间,找不到任何人回答这样一个简单的问题。 Or at least that's what I think it is. 或者至少我是这样认为的。

import PyQt4
from PyQt4 import *

Instead I have to do: 相反,我必须这样做:

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtNetwork import *

Plus a bunch more. 再加上一堆。 Any shortcuts to grab them all? 有什么捷径可以抓住它们吗?

Edit : My solutions was to go through the libraries that I was using (for Py2exe) and just copy their imports. 编辑 :我的解决方案是浏览我正在使用的库(对于Py2exe)并仅复制它们的导入。 That worked. 那行得通。 Still, but not as tedious. 仍然,但不是那么乏味。

Don't use import * , namespaces exist for a good reason. 不要使用import * ,命名空间存在是有充分理由的。

Import the modules you need, in the modules you need them in. 在需要的模块中导入所需的模块。

This code might require a few extra letters, but you'd only know QUrl was a Qt module by convention, not surety. 这段代码可能需要一些额外的字母,但是按照惯例,您只会知道QUrl是Qt模块,而不是保证对象。

 from PyQt import QtCore
 u = QtCore.QUrl

The above code means when you are examining the second line you know for certain that it is a Qt object from a specific module and nothing else. 上面的代码意味着当您检查第二行时,可以肯定地知道它是来自特定模块的Qt对象,而没有别的什么。

如果要将所有PyQt4类导入一个名称空间,则可以执行以下操作:

from PyQt4 import Qt

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

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