python - QApplication 实例已经存在

标签 python pyside maxscript

我正在 3Dsmax 2015 上做一些简单的 PySide。

这是我的错误:

python.ExecuteFile "C:\Program Files\Autodesk\3ds Max 2015\scripts\Python\demoUniTest.py"
-- Runtime error:  Line 32  <module>()
  <type 'exceptions.RuntimeError'> A QApplication instance already exists.

这是我的代码:

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

当我在 Pycharm 上使用这段代码时,我没有收到任何错误。只有当我在 3Dsmax 2015 Listener 上使用它时才会出现

最佳答案

帮助文件中的直接引用 ( 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()

关于python - QApplication 实例已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27150852/

相关文章:

python - 如何创建一个根据多种条件增加的ID?

python - Maxscript Python 添加修饰符

python - QTreeView标题显示文本

python - 默认情况下使用 QTabWidget 防止使用 Ctrl+Tab 进行选项卡循环

math - 非规范化向量

python - context.get ('active_ids' , []) 是什么意思?我们为什么用它?

python - python3中的__new__在哪里定义的?

python - end =' ' 究竟是做什么的?

python - pyside 代码行 'combo.activated[str].connect(self.onActivated)' 中括号的含义是什么?