在字符串中使用变量 - 特别是 Python/Maya 中的样式表
Using variables in a string - specifically stylesheets in Python/Maya
这是对我在 Maya 中更改颜色的问题的跟进 UI。我很幸运地获得了一些重要信息,并继续练习我试图在样式表中为 RGB 浮点值添加一个变量。
我学习了很多东西并意识到我本质上是在尝试在字符串中使用 int 或 float,所以我制作了一个随机的 float 变量列表,我认为我可以将其用于 "color" 属性 而不是 "rgb"。到目前为止,任何尝试将数据与 .format 连接起来的尝试似乎都无法正常工作。仍在谷歌搜索答案,但我想我会问是否有人可以指出我正确的方向。
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 值或设置 "color: (color name)" 来手动分配颜色,但我认为看看如何在这样的字符串中使用变量会很有趣。
谢谢!
亚历克斯
您遇到的问题是由于字符串中的文字花括号与占位符值周围的花括号混淆了。您需要用连续的两个大括号替换它们,以告诉 python 将它们视为文字大括号:
editor_win.setStyleSheet("scriptEditorFramePanel {{border: 13px solid color{0}}}".format(color2)
这是对我在 Maya 中更改颜色的问题的跟进 UI。我很幸运地获得了一些重要信息,并继续练习我试图在样式表中为 RGB 浮点值添加一个变量。
我学习了很多东西并意识到我本质上是在尝试在字符串中使用 int 或 float,所以我制作了一个随机的 float 变量列表,我认为我可以将其用于 "color" 属性 而不是 "rgb"。到目前为止,任何尝试将数据与 .format 连接起来的尝试似乎都无法正常工作。仍在谷歌搜索答案,但我想我会问是否有人可以指出我正确的方向。
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 值或设置 "color: (color name)" 来手动分配颜色,但我认为看看如何在这样的字符串中使用变量会很有趣。
谢谢! 亚历克斯
您遇到的问题是由于字符串中的文字花括号与占位符值周围的花括号混淆了。您需要用连续的两个大括号替换它们,以告诉 python 将它们视为文字大括号:
editor_win.setStyleSheet("scriptEditorFramePanel {{border: 13px solid color{0}}}".format(color2)