问题描述
例如,对于表单的每一行,我要指定:
- 第一行对 label+textField 使用 2 列布局
- 第 2 行使用 1 列布局作为菜单项,该菜单项有自己的注释标签
- 第三行向 1 列布局添加了一个全角执行按钮
- 如果用户调整窗口大小,所有控件都会适当缩放
TIA!
import pymel.core as pm
def test(*args):
print('model name: {}'.format(modelTField.getText()))
print('model geom: {}'.format(modelMenu.getValue()))
if pm.window("testUI",ex=1): pm.deleteUI("testUI")
window = pm.window("testUI",t="Test v0.1",w=500,h=200)
mainLayout = pm.verticalLayout()
# two column layout
col2Layout = pm.horizontalLayout(ratios=[1,2],spacing=10)
pm.text(label='Model name')
global modelTField
modelTField = pm.textField()
col2Layout.redistribute()
# single column layout
col1Layout = pm.horizontalLayout()
global modelMenu
modelMenuName = "modelMenu"
modelMenuLabel = "Model mesh"
modelMenuAnnotation = "Select which geo corresponds to the model shape"
modelMenu = pm.optionMenu(modelMenuName,l=modelMenuLabel,h=20,ann=modelMenuAnnotation)
pm.menuItem(l="FooShape")
pm.menuItem(l="BarShape")
# execute
buttonLabel = "[DOIT]"
button = pm.button(l=buttonLabel,c=test)
col2Layout.redistribute()
# display window
pm.showWindow(window)
解决方法
由于 pm.horizontalLayout()
和 pm.verticalLayout
会自动重新分发内容,因此您无需自己调用重新分发。但是要使这些函数正常工作,您需要像这样的 with
语句:
import pymel.core as pm
def test(*args):
print('model name: {}'.format(modelTField.getText()))
print('model geom: {}'.format(modelMenu.getValue()))
if pm.window("testUI",ex=1): pm.deleteUI("testUI")
window = pm.window("testUI",t="Test v0.1",w=500,h=200)
with pm.verticalLayout() as mainLayout:
with pm.horizontalLayout(ratios=[1,2],spacing=10) as col2Layout:
pm.text(label='Model name'
global modelTField
modelTField = pm.textField()
with pm.horizontalLayout() as col1Layout:
global modelMenu
modelMenuName = "modelMenu"
modelMenuLabel = "Model mesh"
modelMenuAnnotation = "Select which geo corresponds to the model shape"
modelMenu = pm.optionMenu(modelMenuName,l=modelMenuLabel,h=20,ann=modelMenuAnnotation)
pm.menuItem(l="FooShape")
pm.menuItem(l="BarShape")
# execute
buttonLabel = "[DOIT]"
button = pm.button(l=buttonLabel,c=test)
# display window
pm.showWindow(window)
要使用 python 创建 UI,如果将所有内容都包含在一个类中会很有帮助,尤其是在使用 PyMel 时。你可以这样做:
class MyWindow(pm.ui.Window):
def __init(self):
self.title = "TestUI"
def buttonCallback(self,*args):
do something....
如果您有回调,这将非常有用,因为您需要的一切都可以从类中访问,而且您根本不需要全局变量。