在Spyder中使用app.exec()而不是sys.exit(app.exec_())与PyQT4将无法正常工作

时间:2016-03-21 02:22:49

标签: pyqt4 spyder

我正在使用Spyder IDE(使用IPython)在Windows 7机器上开发PyQT程序,我在使用语句sys.exit(app.exec_())时遇到问题。我看过这篇文章

What the error when I close the dialog

并尝试使用app.exec_()。但是,当我只使用app.exec_()时,GUI会非常短暂地打开,然后立即关闭。这是我的最小(非)工作示例:

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        btn = QtGui.QPushButton('Button', self)   
        self.show()

def main():
    app = QtGui.QApplication(sys.argv)
    ex = Example()
    app.exec_()
    #sys.exit(app.exec_())

if __name__ == '__main__':
    main()

这是我关于stackoverflow的第一篇文章,所以如果我能以任何方式改进这篇文章,请告诉我。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。显然,根据

,您根本不需要包含app.exec_()

http://cyrille.rossant.net/making-pyqt4-pyside-and-ipython-work-together/

以下代码适用于Spyder:

import sys
from PyQt4 import QtGui

class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()
        self.initUI()

    def initUI(self):
        btn = QtGui.QPushButton('Button', self)   
        self.show()

ex= Example()