QT Creator 将 ui 转换为在 windows 终端中工作但不与 spyder IDE 一起工作的 py
QT Creator converted ui to py working in windows terminal but not working with spyder IDE
我正在学习使用 QT Creator 和 python 制作基本 GUI 的教程。我已经成功地创建了一个 window,它带有一个按钮,按下时会关闭 window。它在 QT Creator 中工作正常,我将 ui 文件转换为 py,它 运行s 并在我打开命令提示符 window 并调用它时按预期创建 window python main.py
。我喜欢在 spyder IDE 工作,并希望继续这样做。问题是,如果我在 spyder 和 运行 中打开 main.py,它将首先给出错误
An exception has occurred, use %tb to see the full traceback.
SystemExit: -1
然后如果我再次尝试 运行 内核将挂起。
什么是 required 到 运行 这个脚本在 spyder 中成功?这是代码:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(400, 300)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.centralWidget)
self.horizontalLayout.setMargin(11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton = QtGui.QPushButton(self.centralWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Close", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
没有实际的回溯很难说。我猜这是两件事之一,都与 Spyder 也是基于 Qt 构建的事实有关。
您正试图在 Spyder 环境中 运行 您的应用程序。您的应用程序尝试创建 QApplication
,但由于 Spyder 已经 运行 在 Qt
上运行,因此 QApplication
已经存在并且出错。
PyQt/Qt 的版本不匹配。我假设 Spyder 附带它自己的 Qt/PyQt 版本,您不必将它安装到 运行 Spyder。但我猜你也确实安装了你自己的 Qt/PyQt 版本并且 PyQt 正在加载错误的 dll(Spyder 附带的而不是你安装的)。
一些要检查的东西:
检查以确保您从 Spyder 启动外部进程(即在一个全新的 shell 和一个新的 python 进程中)而不是简单地 运行在 Spyder 嵌入式 python 进程中设置代码。我不太了解 Spyder,不知道如何执行此操作,但大多数 IDE 都有一些设置来控制它如何启动外部进程。
检查启动脚本中的 PATH
环境变量。有可能在安装 Qt 之前添加了 Spyder 目录,导致在导入 PyQt 时加载 Spyder Qt dll 而不是系统安装的 Qt。
我正在学习使用 QT Creator 和 python 制作基本 GUI 的教程。我已经成功地创建了一个 window,它带有一个按钮,按下时会关闭 window。它在 QT Creator 中工作正常,我将 ui 文件转换为 py,它 运行s 并在我打开命令提示符 window 并调用它时按预期创建 window python main.py
。我喜欢在 spyder IDE 工作,并希望继续这样做。问题是,如果我在 spyder 和 运行 中打开 main.py,它将首先给出错误
An exception has occurred, use %tb to see the full traceback.
SystemExit: -1
然后如果我再次尝试 运行 内核将挂起。
什么是 required 到 运行 这个脚本在 spyder 中成功?这是代码:
from PyQt4 import QtCore, QtGui
try:
_fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
def _fromUtf8(s):
return s
try:
_encoding = QtGui.QApplication.UnicodeUTF8
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
def _translate(context, text, disambig):
return QtGui.QApplication.translate(context, text, disambig)
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName(_fromUtf8("MainWindow"))
MainWindow.resize(400, 300)
self.centralWidget = QtGui.QWidget(MainWindow)
self.centralWidget.setObjectName(_fromUtf8("centralWidget"))
self.horizontalLayout = QtGui.QHBoxLayout(self.centralWidget)
self.horizontalLayout.setMargin(11)
self.horizontalLayout.setSpacing(6)
self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout"))
self.pushButton = QtGui.QPushButton(self.centralWidget)
self.pushButton.setObjectName(_fromUtf8("pushButton"))
self.horizontalLayout.addWidget(self.pushButton)
MainWindow.setCentralWidget(self.centralWidget)
self.menuBar = QtGui.QMenuBar(MainWindow)
self.menuBar.setGeometry(QtCore.QRect(0, 0, 400, 21))
self.menuBar.setObjectName(_fromUtf8("menuBar"))
MainWindow.setMenuBar(self.menuBar)
self.mainToolBar = QtGui.QToolBar(MainWindow)
self.mainToolBar.setObjectName(_fromUtf8("mainToolBar"))
MainWindow.addToolBar(QtCore.Qt.TopToolBarArea, self.mainToolBar)
self.statusBar = QtGui.QStatusBar(MainWindow)
self.statusBar.setObjectName(_fromUtf8("statusBar"))
MainWindow.setStatusBar(self.statusBar)
self.retranslateUi(MainWindow)
QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), MainWindow.close)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow", None))
self.pushButton.setText(_translate("MainWindow", "Close", None))
if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
MainWindow = QtGui.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
没有实际的回溯很难说。我猜这是两件事之一,都与 Spyder 也是基于 Qt 构建的事实有关。
您正试图在 Spyder 环境中 运行 您的应用程序。您的应用程序尝试创建
QApplication
,但由于 Spyder 已经 运行 在Qt
上运行,因此QApplication
已经存在并且出错。PyQt/Qt 的版本不匹配。我假设 Spyder 附带它自己的 Qt/PyQt 版本,您不必将它安装到 运行 Spyder。但我猜你也确实安装了你自己的 Qt/PyQt 版本并且 PyQt 正在加载错误的 dll(Spyder 附带的而不是你安装的)。
一些要检查的东西:
检查以确保您从 Spyder 启动外部进程(即在一个全新的 shell 和一个新的 python 进程中)而不是简单地 运行在 Spyder 嵌入式 python 进程中设置代码。我不太了解 Spyder,不知道如何执行此操作,但大多数 IDE 都有一些设置来控制它如何启动外部进程。
检查启动脚本中的
PATH
环境变量。有可能在安装 Qt 之前添加了 Spyder 目录,导致在导入 PyQt 时加载 Spyder Qt dll 而不是系统安装的 Qt。