简体   繁体   中英

Maya Python: Button always at the center of the window

I'm starting experimenting with Maya python, and I'm trying to do some UI. I came across to a really strange problem, I can't get a button to stay in the center of the windows. I've tried different things but nothing seems to work, here is the code:

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()

this is a simple version but still, I can't get it to work. can someone help me?

I honestly don't know the answer as anytime I have to dig into Maya's native UI stuff it makes me question my own life.

So I know it's not exactly what you're asking for, but I'll opt with this: Use PySide instead. At first glance it might make you go "woah, that's way too hard", but it's also a million times better (and actually easier). It's much more powerful, flexible, has great documentation, and also used outside of Maya (so actually useful to learn). Maya's own interface uses the same framework, so you can even edit it with PySide once you're more comfortable with it.

Here's a bare-bones example to create a centered button in a window:

# 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!

Not too bad, right?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM