问题
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()
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.
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?
回答1:
I had the same problem. These steps worked for me.
- 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.
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.
回答2:
For all those lonesome internet wanderers trying to figure out how to integrate eclipse, pydev, and pyqt5 on Linux, I bring you my method from start to finish.
Eclipse, PyQt5, and PyDev on Linux
- Install python v3.6
- Install eclipse from eclipse.org
- In eclipse, click Help->Install New Software
- Click Add...
- Add in software source "http://www.PyDev.org/updates" to the available software sources
- Call it PyDev
- Click on PyDev checkbox
- Install it by clicking Next
- Download PyQt5
- Download SIP
- Install SIP first
- Install PyQt5
- Reconfigure eclipse to use PyQt5
- Click on Window→Preferences→PyDev→Interpreters→Python Interpreters
- Click on Advanced Auto-Config
- Rename interpreter to “python3.6”
- Click on Libraries tab
- Click on New Folder
- Add in “/usr/lib/x86_64-linux-gnu/qt5/plugins”
- Add in “/usr/lib/x86_64-linux-gnu/qt5/libexec”
- Add in “/usr/lib/x86_64-linux-gnu/qt5/bin”
- Click Apply
- Click Apply and Close
- Restart eclipse
- Profit!
This will allow you to get the tab code completion in eclipse when developing pyqt5 applications.
来源:https://stackoverflow.com/questions/31101925/pydev-doesnt-recognise-pyqt5