コンテンツにスキップ

重複したウィンドウ作成の回避

Windowの重複表示を回避します。
参考:
1. https://snippets.cacher.io/snippet/41b95566018f38e3d717
2. https://fereria.github.io/reincarnation_tech/11_PySide/02_Tips/01_child_dup_window/#_1

参考1. の以下のコードで、line46~49
が、これに該当する。
closeWindow.py

## -*- coding: utf-8 -*-
# *----------------------------------------------*#
# 各種モジュール読み込み
# *----------------------------------------------*#

from PySide2 import QtCore, QtGui, QtWidgets

from maya import OpenMayaUI as omUI
import shiboken2


# *----------------------------------------------*#
# MAIN
# *----------------------------------------------*#
def getMayaWindow():
    """
    Get the main Maya window as a QtGui.QMainWindow instance
    @return: QtGui.QMainWindow instance of the top level Maya windows
    """
    ptr = omUI.MQtUtil.mainWindow()
    if ptr is not None:
        return shiboken2.wrapInstance(long(ptr), QtWidgets.QMainWindow)


class testUI(QtWidgets.QDialog):

    def __init__(self):
        super(testUI,self).__init__(getMayaWindow())
        # すでにUIが出来てる場合は削除する #
        # MayaのMainWindowの子Windowを取得
        child_list =  self.parent().children()
        for c in child_list:
            # 自分と同じ名前のUIのクラスオブジェクトが存在してたらCloseする
            if self.__class__.__name__ == c.__class__.__name__:
                c.close()
        # ------------------------ #
        btn = QtWidgets.QPushButton("Hello World")
        layout = QtWidgets.QVBoxLayout()

        layout.addWidget(btn)
        self.setLayout(layout)


def main():
    mainWindow = getMayaWindow()
    nowExists = mainWindow.findChildren(testUI)

    for i in nowExists:
        i.close()

    win = testUI()
    win.show()


main()


最終更新日: 2024年4月18日 09:56:46