这是对我在Maya UI中更改颜色的问题的跟进。我很幸运获得了一些很好的信息,并且继续练习,我试图在样式表中为RGB float值添加一个变量。
我一直在学习一堆东西,意识到我本质上是在尝试在字符串中使用int或float,所以我随机列出了一个float变量列表,以为可以用于“ color”属性而不是“ rgb” 。到目前为止,任何尝试使用.format连接数据的尝试似乎都无法实现。仍在Google搜索中寻找答案,但以为我会问是否有人可以指出我正确的方向。
gMainWindow = maya.mel.eval('$tmpVar=$gMainWindow')
import random
import shiboken2
from maya import cmds
from maya import OpenMayaUI
from PySide2 import QtWidgets
from PySide2.QtCore import *
from PySide2.QtWidgets import *
def clamp(num, min_value, max_value):
return max(min(num, max_value), min_value)
r = random.randrange(0,255)
g = random.randrange(0,255)
b = random.randrange(0,255)
rgb = [r,g,b]
colorRGB = ['Red', 'White', 'Blue']
color2= random.choice(colorRGB)
window = cmds.window(gMainWindow, edit=True, backgroundColor=(r,g,b))
panels = cmds.getPanel(scriptType="scriptEditorPanel") # Get all script editor panel names.
if panels: # Make sure one actually exists!
script_editor_ptr = OpenMayaUI.MQtUtil.findControl(panels[0]) # Grab its pointer with its internal name.
script_editor = shiboken2.wrapInstance(long(script_editor_ptr), QtWidgets.QWidget) # Convert pointer to a QtWidgets.QWidget
editor_win = script_editor.parent().parent().parent().parent() # Not very pretty but found that this was the best object to color with. Needed to traverse up its parents.
editor_win.setObjectName("scriptEditorFramePanel") # This object originally had no internal name, so let's set one.
#editor_win.setStyleSheet("scriptEditorFramePanel {border: 13px solid rgb(155,155,155);}") # Set its styleSheet with its internal name so that it doesn't effect any of its children.
editor_win.setStyleSheet("scriptEditorFramePanel {border: 13px solid color{0}}".format(color2)
我专门尝试更改此行:
editor_win.setStyleSheet("scriptEditorFramePanel {border: 13px solid rgb(155,155,155);}")
我可以通过更改RGB值或设置“颜色:(颜色名称)”来手动分配颜色,但是我认为看到如何在这样的字符串中使用变量会很有趣。
谢谢!亚历克斯
您遇到的问题是由于字符串中的文字花括号与占位符值引起的混淆。您需要连续用两个大括号替换它们,以告诉python将它们视为文字大括号:
editor_win.setStyleSheet("scriptEditorFramePanel {{border: 13px solid color{0}}}".format(color2)