我在PyQt4中打开新窗口时如何关闭旧窗口

时间:2018-02-09 03:27:16

标签: python pyqt pyqt4 maya

我在Maya中使用PyQt4和来自互联网的代码创建了一个modelEditor,但是当我再次运行这个脚本时,我不知道如何关闭它。我尝试使用sys.exit(app.exec_()),但这不起作用。

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui 
import maya.cmds as cmds
import maya.OpenMayaUI as mui 
import sip
import sys
global app
class MyDialog(QtGui.QDialog):

    def __init__(self, parent, **kwargs):
        super(MyDialog, self).__init__(parent, **kwargs)

        self.setObjectName("MyWindow")
        self.resize(800, 600)
        self.setWindowTitle("PyQt ModelPanel Test")

        self.verticalLayout = QtGui.QVBoxLayout(self)        
        self.verticalLayout.setObjectName("mainLayout")
        layout = mui.MQtUtil.fullName(long(sip.unwrapinstance(self.verticalLayout)))
        cmds.setParent(layout)

        self._cameraName = cmds.camera()[0]
        self.newEditor = cmds.modelEditor()
        nodeName = cmds.modelEditor(self.newEditor,cam=self._cameraName,edit = True,hud = 0,alo = 0,pm = 1,da = "smoothShaded")        
        ptr = mui.MQtUtil.findControl(nodeName)        
        self.modelPanel = sip.wrapinstance(long(ptr), QtCore.QObject)

        self.verticalLayout.addWidget(self.modelPanel)

    def show(self):
        super(MyDialog, self).show()        
        self.modelPanel.repaint()

def show():
    if (cmds.window(MyDialog,ex = True)):
        cmds.deleteUI(MyDialog)

    global app    
    app = QtGui.QApplication.instance()    
    ptr = mui.MQtUtil.mainWindow()    
    win = sip.wrapinstance(long(ptr), QtCore.QObject)
    d = MyDialog(win)
    d.show()

1 个答案:

答案 0 :(得分:1)

这是你可以采取的一种方式。 有一个类单例,它将保存类的实例。然后在构造函数中,您可以检查实例是否存在;如果是,则删除它并重置变量。

# -*- coding: utf-8 -*-
from PyQt4 import QtCore, QtGui 
import maya.cmds as cmds
import maya.OpenMayaUI as mui 
import sip
import sys
global app

class MyDialog(QtGui.QDialog):

    instance = None # This will contain an instance of this class.

    def __init__(self, parent, **kwargs):
        # Delete any existing instance, then set this as the current instance.
        self.delete_instance()
        self.__class__.instance = self

        super(MyDialog, self).__init__(parent, **kwargs)

        self.setObjectName("MyWindow")
        self.resize(800, 600)
        self.setWindowTitle("PyQt ModelPanel Test")

        self.verticalLayout = QtGui.QVBoxLayout(self)        
        self.verticalLayout.setObjectName("mainLayout")
        layout = mui.MQtUtil.fullName(long(sip.unwrapinstance(self.verticalLayout)))
        cmds.setParent(layout)

        self._cameraName = cmds.camera()[0]
        self.newEditor = cmds.modelEditor()
        nodeName = cmds.modelEditor(self.newEditor,cam=self._cameraName,edit = True,hud = 0,alo = 0,pm = 1,da = "smoothShaded")        
        ptr = mui.MQtUtil.findControl(nodeName)        
        self.modelPanel = sip.wrapinstance(long(ptr), QtCore.QObject)

        self.verticalLayout.addWidget(self.modelPanel)

    def delete_instance(self):
        if self.__class__.instance is not None:
            try:
                self.__class__.instance.deleteLater()
            except Exception as e:
                pass

    def show(self):
        super(MyDialog, self).show()        
        self.modelPanel.repaint()

def show():
    global app
    app = QtGui.QApplication.instance()    
    ptr = mui.MQtUtil.mainWindow()    
    win = sip.wrapinstance(long(ptr), QtCore.QObject)
    d = MyDialog(win)
    d.show()

请注意,如果您打开了一个实例并且reload()该模块,则它不会删除已打开的实例,因为该类会有所不同!