Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
729 views
in Technique[技术] by (71.8m points)

python - A QApplication instance already exists

I'm doing some simple PySide on 3Dsmax 2015.

This is my error:

python.ExecuteFile "C:Program FilesAutodesk3ds Max 2015scriptsPythondemoUniTest.py"
-- Runtime error:  Line 32  <module>()
  <type 'exceptions.RuntimeError'> A QApplication instance already exists.

This is my code:

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

class Form(QDialog):
def __init__(self,parent=None):
    super(Form,self).__init__(parent)

    self.browser = QTextBrowser()
    self.lineedit = QLineEdit("Type an expression and press Enter")
    self.lineedit.selectAll()

    layout = QVBoxLayout()
    layout.addWidget(self.browser)
    layout.addWidget(self.lineedit)
    self.setLayout(layout)

    self.lineedit.setFocus()

    self.connect(self.lineedit, SIGNAL("returnPressed()"),self.updateUi)
    self.setWindowTitle("Calculate")

def updateUi(self):
    try:
        text = self.lineedit.text()
        self.browser.append("%s = <b>%s</b>" % (text,eval(text)))
    except:
        self.browser.append("<font color=red>%s is invalid</font>" %text)

app = QApplication(sys.argv)

form = Form()

form.show()

app.exec_()

When I use this code on Pycharm,I don't get any errors. It only appears when I use it on 3Dsmax 2015 Listener

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Direct citation from the helpfile (Using PySide):

Normally one creates a PySide application object in a script using QtGui.QApplication(). However, in 3ds Max, there is already a PySide application running, so you get a handle for that object like this:

QtGui.QApplication.instance()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...