Getting PySide Hello App to run under Canopy

2019-06-06 09:14发布

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条回答
兄弟一词,经得起流年.
2楼-- · 2019-06-06 10:04

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"
查看更多
登录 后发表回答