Maya Python:按钮始终位于窗口的中心

问题描述

我开始尝试使用Maya python,并且尝试做一些UI。 我遇到了一个非常奇怪的问题,我无法让按钮停留在窗户中央。 我尝试了不同的方法,但似乎没有任何效果代码如下:

import maya.cmds as cmds
cmds.window( width=200 )
WS = mc.workspaceControl("dockName",retain = False,floating = True,mw=80)

submit_widget = cmds.rowLayout(numberOfColumns=1,p=WS)

cmds.button( label='Submit Job',width=130,align='center',p=submit_widget)

cmds.showWindow()

这是一个简单的版本,但仍然无法正常工作。 有人可以帮我吗?

解决方法

老实说,我不知道答案,因为无论何时我不得不深入研究Maya的本机UI东西,这都会使我质疑自己的生活。

所以我知道这并不是您要的,但我会选择这样做:使用PySide代替。乍一看,它可能会让您“哇,这太难了”,但它又好了一百万倍(实际上更容易)。它功能更强大,更灵活,文档完善,而且还可以在Maya之外使用(因此非常有用)。 Maya自己的界面使用相同的框架,因此,一旦您更熟悉它,甚至可以使用PySide对其进行编辑。

这是在窗口中创建居中按钮的简单示例:

# Import PySide libraries.
from PySide2 import QtCore
from PySide2 import QtWidgets


class MyWindow(QtWidgets.QWidget):  # Create a class for our window,which inherits from `QWidget`
    
    def __init__(self,parent=None):  # The class's constructor.
        super(MyWindow,self).__init__(parent)  # Initialize its `QWidget` constructor method.
        
        self.my_button = QtWidgets.QPushButton("My button!")  # Create a button!
        
        self.my_layout = QtWidgets.QVBoxLayout()  # Create a vertical layout!
        self.my_layout.setAlignment(QtCore.Qt.AlignCenter)  # Center the horizontal alignment.
        self.my_layout.addWidget(self.my_button)  # Add the button to the layout.
        self.setLayout(self.my_layout)  # Make the window use this layout.
        
        self.resize(300,300)  # Resize the window so it's not tiny.


my_window_instance = MyWindow()  # Create an instance of our window class.
my_window_instance.show()  # Show it!

还不错吧?