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!

还不错吧?

最新更新