我讨厌给UI起名字,也懒得起名字

从我开始给Maya开发UI工具的时候起就觉得给窗口起名是件麻烦的事情,因为Maya不允许有两个同名的窗口存在,所以要起一个独一无二的名字,我表示这让我很蛋疼.
我喜欢用类来编写UI,因为它让我从此不再蛋疼.
from maya import cmds
class MyWindow(object):
    
    window = None
    
    def __init__(self):
        """
        do what you want
        """
        pass
    
    def _createWindow(self):
        """
        create the UI
        """
        if MyWindow.window is not None \
        and cmds.window(MyWindow.window, q=1, ex=1):
            cmds.deleteUI(MyWindow.window)
        
        MyWindow.window = cmds.window(t="This is a window")
        cmds.columnLayout()
        cmds.text(l="The name of this Window is :%s" % MyWindow.window)
    
    def show(self):
        """
        show window
        """
        self._createWindow()
        cmds.showWindow(MyWindow.window)

# show our window
# 创建窗口
mywindow = MyWindow()
mywindow.show()

当你的窗口被关闭了,只要再执行
mywindow = MyWindow()
mywindow.show()

就行了,你永远都只会显示一个窗口.

你可能感兴趣的:(UI,python,Maya)