简体   繁体   English

Maya (PySide2) – 重新打开 window 而不是新的 window

[英]Maya (PySide2) – Reopen window instead of a new window

I having the problem that every time I press the hotkey(in this case CTRL + A) I open a new window and the old one does not disappear.我遇到的问题是每次按下热键(在本例中为 CTRL + A)我打开一个新的 window 并且旧的不会消失。

I have added a random color and random position into the code so you the new windows.我在代码中添加了随机颜色和随机 position,所以你是新的 windows。 Use the script in Maya:在 Maya 中使用脚本:

from PySide2 import QtWidgets, QtCore, QtGui
from PySide2.QtGui import QKeySequence
from PySide2.QtWidgets import QShortcut
from maya import OpenMayaUI
import sys
from random import randrange

try:
    from shiboken import wrapInstance
    import shiboken
except:
    from shiboken2 import wrapInstance
    import shiboken2 as shiboken

class MainWindow(QtWidgets.QMainWindow):
    def __init__(self, parent = None):
        window = OpenMayaUI.MQtUtil.mainWindow()
        mayaWindow = shiboken.wrapInstance(long(window), QtWidgets.QMainWindow)
        super(MainWindow, self).__init__(mayaWindow)

        self.setWindowTitle('Test Window')

        #Set a random color:
        self.setStyleSheet('background-color: rgb(' + str(randrange(255)) + ', ' + str(randrange(255)) + ', ' + str(randrange(255)) + ');')

        self.setWindowFlags(QtCore.Qt.Popup | QtCore.Qt.WindowType.NoDropShadowWindowHint)
        self.resize(630, 400)
        self.releaseKeyboard()
        self.releaseMouse()
        self._window = None

        #Set a random position:
        self.setGeometry(randrange(800), randrange(800), randrange(800), randrange(800))

        # main widget
        mainWidget = QtWidgets.QWidget(self)
        self.setCentralWidget(mainWidget)


    def mousePressEvent(self, QMouseEvent):
        closeOnLostFocus = True

        if closeOnLostFocus:
            xPosition = QMouseEvent.pos().x()
            yPosition = QMouseEvent.pos().y()
            width = self.width()
            height = self.height()
            if xPosition > self.width() or xPosition < 0:
                self.closeWindow()
            if yPosition > self.height() or yPosition < 0:
                self.closeWindow()


    def showWindow(self):
        self.closeWindow()
        if self._window is None:
            self._window = MainWindow()
            self._window.show()


    def closeWindow(self):
        self.close()


def setHotkey(*args):
    hotkey = ''

    window = window = OpenMayaUI.MQtUtil.mainWindow()
    mayaWindow = shiboken.wrapInstance(long(window), QtWidgets.QMainWindow)
    hotkey = 'CTRL + A'
    shortcut = QShortcut(QKeySequence(QtCore.Qt.CTRL + QtCore.Qt.Key_A), mayaWindow)
    shortcut.setContext(QtCore.Qt.ApplicationShortcut)
    shortcut.activated.connect(startApp)
    return hotkey


def startApp(*args):
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    mainWindow = MainWindow()
    mainWindow.showWindow()

setHotkey()

I would like that a new window appears and the one created before that disappears, so that only one exists at a time.我希望出现一个新的 window 而之前创建的那个消失,这样一次只存在一个。

You have to have a single variable that stores the only object, that's why I have created a global variable:您必须有一个变量来存储唯一的 object,这就是我创建全局变量的原因:

mainWindow = None

def startApp(*args):
    app = QtWidgets.QApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    global mainWindow
    mainWindow = MainWindow()
    mainWindow.show()

setHotkey()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM