嵌入式Python中的PyQt5导入失败

我在我的c ++应用程序中使用pybind11(2.4.2)进行GUI的嵌入式操作。但是,专门导入PyQt5失败。

在带有python script.py的控制台上运行脚本时,一切正常。从c ++ Python API(pybind11)运行无法正常工作,即使导入字典清楚地向我显示了NameError: name 'QtWidgets' is not defined,也显示了错误QtWidgets[...,'PyQt5.QtGui','PyQt5.QtWidgets','PyQt5.uic',...]

from PyQt5 import QtWidgets,uic
from PyQt5.QtCore import QTimer
import numpy as np
import pyqtgraph as pg
import functools

在控制台中仅运行script.py而没有显式python关键字会输出相同的错误。我检查了Windows PATH,但那里只有一个版本的python。 Visual Studio和pybind11一起构建了c ++后端。

我怀疑这与我使用的环境有关,但我无法弄清楚。

编辑:

这是我尝试运行的代码,引发了NameError: name 'QtWidgets' is not defined,它是从pybind11(v2.4.2)的Visual Studio中运行的:

from PyQt5 import QtWidgets,uic
from PyQt5.QtCore import QTimer
import numpy as np
import pyqtgraph as pg
import functools

sys.stderr.write('Python version: {}\n' .format(platform.python_version())) # 3.7.5
sys.stderr.write('Arch: {}\n' .format(struct.calcsize("P") * 8)) # 32
sys.stderr.write('Executable location: {}\n' .format(sys.executable)) # Exe location
sys.stderr.write('System path: {}\n' .format(sys.path))

class MainWindow(QtWidgets.QMainWindow):

    def __init__(self,*args,**kwargs):
        super(MainWindow,self).__init__(*args,**kwargs)

        #Load the UI Page
        uic.loadUi('mainwindow.ui',self)

        x = np.random.normal(size=1000)
        y = np.random.normal(size=1000)

        self.button_start.clicked.connect(self.onClick)

        self.setup()
        self.plot(x,y)
        (...)

def main():
    app = QtWidgets.QApplication(sys.argv)
    main = MainWindow() # Error here -> NameError: name 'QtWidgets' is not defined
    main.show()
    sys.exit(app.exec_())

if __name__ == '__main__':         
    main()
clerkzy 回答:嵌入式Python中的PyQt5导入失败

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3166836.html

大家都在问