PySide2,button无法获取setStyleSheets

2024-10-01 04:48:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在Maya中获得一些PySide2的基础知识,但现在我遇到了一个问题,那个按钮没有得到我想要的translateButtonX.setStyleSheet(...),它只是忽略了它。我试着调试它并注意到,如果我设置window.setStyleSheet(...)而不是从图像,例如从颜色rgb,那么一切都正常。有人知道我错过了什么吗?在

import maya.cmds as cmds
from PySide2 import QtCore
from PySide2 import QtGui
from PySide2 import QtWidgets
import maya.OpenMayaUI as omui

try:
    from shiboken import wrapInstance
except:
    from shiboken2 import wrapInstance


def getMayaWindow():
    pointer = omui.MQtUtil.mainWindow()
    if pointer is not None:
        return wrapInstance(long(pointer), QWidget)

######################################################################
def constraintMaster_UI():
    objectName = 'PyConstraintMasterWin'
    #check if ui already exists or not
    if cmds.window('PyConstraintMasterWin', exists=1):
        cmds.deleteUI('PyConstraintMasterWin',wnd=1)

    #create window#

    parent = getMayaWindow()
    window = QtWidgets.QMainWindow(parent)
    window.setObjectName(objectName)
    window.setWindowTitle('Constraint Master')

    #create main widget#

    mainWidget = QWidget()
    window.setCentralWidget(mainWidget)
    QtWidgets.QStyleFactory.create('Windows')

    #Create main vertical layout#

    verticalLayout = QtWidgets.QVBoxLayout(mainWidget)
    imagePath = cmds.internalVar(upd=1) + 'icons/test.png'
    window.setStyleSheet('background-image:url(' + imagePath + 
    ');border:solid black 1px;')

    #create the translate layout#

    translateLayout = QtWidgets.QHBoxLayout()
    verticalLayout.addLayout(translateLayout)

    #create translate label#

    translateLabel = QtWidgets.QLabel('Translate:')
    translateLayout.addWidget(translateLabel)

    translateButtonX = QtWidgets.QPushButton('X')
    translateLayout.addWidget(translateButtonX)
    translateButtonX.setStyleSheet('background-color:rgb(0, 210, 
    255);border:white 1px;')


    #show the window
    window.show()

constraintMaster_UI()

谨致问候!在


Tags: fromimportifcreatewindowcmdspyside2pointer
1条回答
网友
1楼 · 发布于 2024-10-01 04:48:08

尝试了一个最小的例子,颜色图像都起作用。 可能因为是Maya插件,所以图像的路径不存在,请尝试获取绝对路径,以便确定:

imagePath = os.path.abspath(cmds.internalVar(upd=1) + 'icons/test.png')

那么你就不会在意这个插件是不是从另一个目录执行的。在

相关问题 更多 >