If you are using PyQt5 or PySide2 to customize Maya's user interface, be sure to parent your widget under an existing Maya widget, such as Maya's main window. Otherwise, if the widget is un-parented, it may be destroyed by the Python interpreter's garbage collector if a reference to it is not maintained.
The following code sample exemplifies this best practice. Note that this code also works by importing the PyQt5 modules instead of PySide2.
from maya import OpenMayaUI as omui from PySide2.QtCore import * from PySide2.QtGui import * from PySide2.QtWidgets import * from PySide2 import __version__ from shiboken2 import wrapInstance mayaMainWindowPtr = omui.MQtUtil.mainWindow() mayaMainWindow= wrapInstance(long(mayaMainWindowPtr), QWidget) # WORKS: Widget is fine hello = QLabel("Hello, World", parent=mayaMainWindow) hello.setObjectName('MyLabel') hello.setWindowFlags(Qt.Window) # Make this widget a standalone window even though it is parented hello.show() hello = None # the "hello" widget is parented, so it will not be destroyed. # BROKEN: Widget is destroyed hello = QLabel("Hello, World", parent=None) hello.setObjectName('MyLabel') hello.show() hello = None # the "hello" widget is not parented, so it will be destroyed.
See Writing Workspace controls for details on how to write PyQt and PySide widgets that are compatible with Maya workspaces.