让 PySide Hello App 在 Canopy 下运行 [英] Getting PySide Hello App to run under Canopy

查看:72
本文介绍了让 PySide Hello App 在 Canopy 下运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一位 Canopy 用户在这里学习 PySide.当我运行下面的演示代码时,QApplication 抱怨事件循环已经在运行.'

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?

推荐答案

是的,Pylab 是 IPython 的一种模式,它为 IPython 前端启动一个事件循环,以便您可以在 IPython 命令行与您的 GUI 进行交互.

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.

这是一个简单的代码示例,无论有没有 Pylab 都可以运行.

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"

这篇关于让 PySide Hello App 在 Canopy 下运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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