在 spyder 中从一个提示运行两次 pyqt 应用程序 [英] Running a pyqt application twice from one prompt in spyder

查看:62
本文介绍了在 spyder 中从一个提示运行两次 pyqt 应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 spyder 中运行了一个 pyqt4 应用程序,我用 QtGui.QMainWindow.close() 退出,然后它返回到 spyder python 解释器 提示.但是,如果我再次尝试运行应用程序 runfile('C:/Python33/~/qtapp.py', wdir=r'C:/Python33/~/Appdir') 窗口没有'显示.我必须关闭 python 解释器窗口并打开一个新窗口,然后才能再次运行我的 pyqt4 应用程序.这向我表明我是.

I run a pyqt4 application in spyder, I exit with QtGui.QMainWindow.close() and it returns me to the spyder python interpreter prompt. However, if I try and run the application again runfile('C:/Python33/~/qtapp.py', wdir=r'C:/Python33/~/Appdir') The window doesn't show. I have to shut the python interpreter window down and open a new one up before I can run my pyqt4 app again. This suggest to me that I am.

  1. 未正确关闭应用
  2. 未正确运行应用

我希望能够从同一提示运行 pyqt4 应用程序,这将加快我的开发时间

I want to be able to run the pyqt4 app from the same prompt, this would speed up my dev time

示例代码如下:

from PyQt4 import QtCore, QtGui, Qwt5
import sys

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("MainWindow")
        MainWindow.resize(200, 200)
        self.checkBox = QtGui.QCheckBox(MainWindow)
        self.checkBox.setGeometry(QtCore.QRect(100, 100, 70, 17))
        self.checkBox.setObjectName("checkBox")


        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "Dialog",None, QtGui.QApplication.UnicodeUTF8))
        self.checkBox.setText(QtGui.QApplication.translate("MainWindow", "CheckBox", None, QtGui.QApplication.UnicodeUTF8))



class MainWindow(QtGui.QMainWindow,Ui_MainWindow):

    def __init__(self):
        super(MainWindow, self).__init__()     
        self.setupUi(self)


app = QtGui.QApplication(sys.argv)
form = MainWindow()
form.show()
app.exec_()

当我运行后,窗口出现,再次运行后,窗口不出现,这是我的版本信息:

After I run it once the window shows up, after I run it again the window doesn't show up, Here's my version info:

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 位 (Intel)] 在 win32 上输入帮助"、版权"、信用"或许可"以了解更多信息.

Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information.

导入 NumPy 1.7.1、SciPy 0.12.0、Matplotlib 1.3.0 + guidata 1.6.1、guiqwt 2.3.1输入科学"了解更多详情.

Imported NumPy 1.7.1, SciPy 0.12.0, Matplotlib 1.3.0 + guidata 1.6.1, guiqwt 2.3.1 Type "scientific" for more details.

推荐答案

要在 Spyder 中再次运行 PyQt 应用程序,必须删除/销毁正在运行的应用程序但我们不能使用 sys.exit() 因为它会尝试关闭 Python.对我有用的一种解决方案(Python 3.4.1、Spyder 2.3.5.2、PyQt 4.10.4)是使用 QtCore.QCoreApplication.instance().quit()deleteLater,如本例所示:

To run the PyQt application again in Spyder, the running application must be deleted/destroyed but we can't use sys.exit() because it will try to close Python. One solution that works for me (Python 3.4.1, Spyder 2.3.5.2, PyQt 4.10.4) is to use QtCore.QCoreApplication.instance().quit() and deleteLater as shown in this example:

import sys
from PyQt4 import QtGui, QtCore

class Window(QtGui.QMainWindow):
    """PyQt app that closes successfully in Spyder.
    """
    def __init__(self):
        super().__init__()
        self.setGeometry(200, 100, 400, 300)
        self.button()

    def button(self):
        btn = QtGui.QPushButton('Quit', self)
        btn.setGeometry(150, 125, 100, 50)
        btn.clicked.connect(self.quitApp)

    def quitApp(self):
        QtCore.QCoreApplication.instance().quit()

if __name__ == '__main__':
    app = QtGui.QApplication(sys.argv)
    app.aboutToQuit.connect(app.deleteLater)
    win = Window()
    win.show()
    app.exec_()

这篇关于在 spyder 中从一个提示运行两次 pyqt 应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆