Getting PySide Hello App to run under Canopy

青春壹個敷衍的年華 提交于 2019-12-23 01:54:56

问题


A Canopy user here learning about PySide. When I run the demo code below, QApplication complains the event loop is already running.'

import sys
from PySide.QtCore import *
from PySide.QtGui import *


# Create a Qt application
#app = QApplication(sys.argv) #QApplication complains an instance already exists
app = QApplication.instance() #So we just ask for the instance.

#app.aboutToQuit.connect(app.deleteLater)
# Create a Label and show it
label = QLabel("Hello World")
label.show()
# Enter Qt application main loop
app.exec_()
sys.exit()

So how can I get this simple code to run?


回答1:


Yes, Pylab is a mode of IPython which starts an event loop for the IPython front end so that you can interact at the IPython command line with your GUI.

Here's an simple example of code which will run with or without Pylab.

import sys
from PySide import QtGui
app = QtGui.QApplication.instance()
standalone = app is None
if standalone:
    app = QtGui.QApplication(sys.argv)
wid = QtGui.QWidget()
wid.resize(250,150)
wid.setWindowTitle('Simple')
wid.show()
if standalone:
    sys.exit(app.exec_())
else:
    print "We're back with the Qt window still active"


来源:https://stackoverflow.com/questions/27536518/getting-pyside-hello-app-to-run-under-canopy

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!