2016-03-21 24 views
2

我正在開發一個在Windows 7機器上使用Spyder IDE(使用IPython)的PyQT程序,我在陳述sys.exit(app.exec_())中遇到問題。我已閱讀這篇文章在Spyder中使用app.exec()而不是sys.exit(app.exec_())和PyQT4將不起作用

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() 

這是我的計算器上的第一篇文章,所以如果我能改善這個帖子以任何方式,請讓我知道。

+0

經進一步調查,看來這是一個問題,特別是與Spyder的,而不是一般的IPython。我在Jupyter筆記本上運行了上面的代碼,並且它沒有任何問題地生成GUI。因此,深入瞭解爲什麼在Spyder中不起作用,我們將非常感激。謝謝。 – dyson

回答

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() 
相關問題