本文介绍了PyQt4名称在eclipse中显示为未定义,但运行正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Eclipse 3.7.1与Python编码的最新PyDev加载项。我正在使用PyQt4。在我的文件的顶部,我有:

 从PyQt4.QtCore import * 
从PyQt4.QtGui import *

此外,我在项目浏览器列表中包含PyQt4树。然而,eclipse仍然认为像QMainWindow这样的名称是未定义的。代码运行正常。如何获取eclipse来识别这些名字。



谢谢

解决方案

PyQt实际上是一个C ++ Qt库的包装。所以他们不是 .py 文件和PyDev无法分析他们得到它们是什么。您需要在选项卡中添加 PyQt4 ,以便PyDev可以使用Python shell来查找这些库,并了解它们中的内容。这也将为您提供PyQt的代码完成。



除此之外,通常使用从foo import * 。您将导入您的命名空间内的所有内容,您将不知道哪个来自哪里。此外,你可能会有相互掩饰的名字冲突。虽然PyQt不太可能,但我仍然建议您习惯从PyQt4导入QtGui,QtCore 和引用类,如 QtGui.QMainWindow


I am using Eclipse 3.7.1 with the latest PyDev add-in for Python coding. I am using PyQt4. At the top of my file I have:

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

In addition, I have the PyQt4 tree included in the Project Explorer listing. However, eclipse still thinks the names like QMainWindow are undefined. The code runs fine. How may I get eclipse to recognize those names.

Thanks

解决方案

PyQt is actually a wrapping of C++ Qt libraries. So they are not .py files and PyDev can't analyze them to get what is in them. You need to add PyQt4 in the Forced Builtins tab, so that PyDev can use a Python shell to "look into" those libraries and know what is in them. That will also give you code-completion for PyQt.

Apart from that, it is usually not a good practice to use from foo import *. You'll be importing everything inside your namespace and you wouldn't know which is coming from where. Moreover you might have name clashes that mask each other. Though it is unlikely with PyQt, still I'd suggest you get used to from PyQt4 import QtGui, QtCore and reference classes like QtGui.QMainWindow.

这篇关于PyQt4名称在eclipse中显示为未定义,但运行正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-26 20:38