PyDev无法识别PyQt5
我正在关注有关pyqt的教程,并获得了以下代码:
I am following a tutorial on pyqt, and got this code:
import sys
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
class Example(QWidget):
def __init__(self):
super(Example, self).__init__()
self.initUI()
def initUI(self):
cb = QCheckBox('Show title', self)
cb.move(20, 20)
cb.toggle()
cb.stateChanged.connect(self.changeTitle)
self.setGeometry(300, 300, 250, 150)
self.setWindowTitle('Checkbox')
self.show()
def changeTitle(self, state):
if state == Qt.Checked:
self.setWindowTitle('Checkbox')
else: self.setWindowTitle('Unchecked!')
def main():
app = QApplication(sys.argv)
ex = Example()
sys.exit(app.exec_())
if __name__ == '__main__':
main()
我在Eclipse上使用PyDev.可以说该代码运行良好,但是令人尴尬的是PyDev用红线在Qt/Q下加了下划线,当它悬停时表示未定义变量:< ..>
.如果未定义,那么我的代码如何正确运行?显然,这应该是PyDev的问题.我删除了python解释器(它指向的是python2.7而不是3.4),并将其读为正确的版本;但这没用.有趣的是,它可以识别PyQt4并坚持使用该控件而不是PyQt5.
I'm using PyDev on Eclipse. Suffice it to say that the code runs fine, but what is awkward is that PyDev underlines anything Qt/Q with a red line which when hovered over says Undefined variable: <..>
. If it is undefined then how is it that my code runs without errors? Clearly this ought to be a problem with PyDev. I've removed the python interpreter (it was pointing to python2.7 instead of 3.4) and readded it as the correct version; but that didn't work. Interestingly enough, it recognises PyQt4 and insists on using widgets from that instead of PyQt5.
请大家注意,上面的代码示例来自另一台具有PyQt5的笔记本电脑.这两个项目均来自PyDev,并且都具有Ubuntu 15.04.我在当前计算机上导入项目的过程很可能使PyDev解析了所需的库.关于PyDev为什么不识别PyQt5的问题,有人可以解决吗?
Just so you guys are aware, the code sample above is from another laptop which had PyQt5 as well. Both projects were from PyDev, and both had Ubuntu 15.04. It's possible that my importing of the project on my current machine messed up PyDev parsing the required libraries. Does anyone have a solution as to why PyDev doesn't recognise PyQt5?
我遇到了同样的问题.这些步骤对我有用.
I had the same problem. These steps worked for me.
- 设置环境变量:export QT_API = pyqt5(或任何适当的值)
- 重新启动eclipse,以获取新的环境设置,然后将PyQt5添加到解释器的强制内置列表中(Window-> preferences-> pydev-> interpreters-> python解释器),或在此处查看 http://www.pydev.org/manual_101_interpreter.html 了解更多详细信息.
- Set the environment variable: export QT_API=pyqt5 (or whatever as appropriate)
- restart eclipse so that picks up the new environment setting, and then add PyQt5 to the list of forced builtins for the interpreter (Window->preferences->pydev->interpreters->python interpreters) or look here http://www.pydev.org/manual_101_interpreter.html for more details.
以下SO问题提示我该变量的存在:设置IPythonQtconsole与PyQt5 .在设置它之前,我仅通过在内置文件中添加"PyQt5"就可以完成一些工作,但是,例如,它无法提供完整的完整列表,例如来自PyQt5的 .QtGui import
,即使ipython独立运行也是如此.此外,pydev中的python控制台存在相同的问题,并且从 Ipython.core.completerlib
调用 module_completion("from PyQt5.QtGui import Q")
会产生相同的不完整列表.最后,我猜想由于pydev正在为gui事件循环加载PyQt4(也可以在解释器设置中配置),因此当它尝试自省Qt5模块时会出现名称空间冲突,导致它在构建之前无法使用.完整的完成清单.设置环境变量会导致pydev加载pyqt5而不是默认的pyqt4.我没有检查过,但是以这种方式设置pydev可能会在完成pyqt4引用时遇到问题.
The following SO question tipped me off to the presence of the variable: Setting up IPython Qtconsole with PyQt5. Before I set it, I as able to get some completion to work just by adding 'PyQt5' to the builtins, but it would not, for example, provide the full list of completions to something likefrom PyQt5.QtGui import
, even though ipython stand-alone would. Further, the python console in pydev had the same problem and calling module_completion("from PyQt5.QtGui import Q")
from Ipython.core.completerlib
produced the same incomplete list. In the end, I guessed that since pydev was loading PyQt4 for the gui event loop (also configurable in the interpreter settings), there was a namespace conflict when it tried to introspect the Qt5 modules, causing it to bail out before it could build the full list of completions. Setting the environment variable causes pydev to load pyqt5 instead of the default pyqt4. I haven't checked, but it seems likely that set this way pydev will have problems completing pyqt4 references.