如何获取调用函数的Maya/Qt GUI组件?

2024-09-26 22:45:03 发布

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

我正试图找到一种方法,以某种方式“获取”调用函数的GUI组件。通过这种方式,我可以进一步将代码合并为执行类似任务的组件的可重用部分。我需要一种在Maya的GUI命令和Qt命令中实现这一点的方法。我想我要找的是一个通用的python技巧,如“init”、“文件”、“main”等。如果没有通用的python方法来实现这一点,任何Maya/Qt特有的技巧也很受欢迎。在

下面是一些任意的伪代码,可以更好地解释我要查找的内容:

field1 = floatSlider(changeCommand=myFunction)
field2 = colorSlider(changeCommand=myFunction)

def myFunction(*args):
    get the component that called this function

    if the component is a floatSlider
        get component's value
        do the rest of the stuff

    elif the component is a colorSlider
        get component's color
        do the rest of the stuff

Tags: the方法代码命令技巧get方式组件
1条回答
网友
1楼 · 发布于 2024-09-26 22:45:03

从Gombat的评论中展开,下面是一个如何让通用函数与滑块和数字调整框控件一起工作的示例:

from PySide import QtGui, QtCore

class Window(QtGui.QWidget):
    def __init__(self, parent = None):
        super(Window, self).__init__(parent)

        # Create a slider
        self.floatSlider = QtGui.QSlider()
        self.floatSlider.setObjectName('floatSlider')
        self.floatSlider.valueChanged.connect(self.myFunction)

        # Create a spinbox
        self.colorSpinBox = QtGui.QSpinBox()
        self.colorSpinBox.setObjectName('colorSlider')
        self.colorSpinBox.valueChanged.connect(self.myFunction)

        # Create widget's layout
        mainLayout = QtGui.QHBoxLayout()
        mainLayout.addWidget(self.floatSlider)
        mainLayout.addWidget(self.colorSpinBox)
        self.setLayout(mainLayout)

        # Resize widget and show it
        self.resize(300, 300)
        self.show()

    def myFunction(self):
        # Getting current control calling this function with self.sender()
        # Print out the control's internal name, its type, and its value
        print "{0}: type {1}, value {2}".format( self.sender().objectName(), type( self.sender() ), self.sender().value() )

win = Window()

我不知道你想要什么样的控件colorSlider(我不认为PySide的滑块与Maya中的相同,您可能需要自定义它或使用QColorDialog)。但这应该能让你大致了解如何去做。在

相关问题 更多 >

    热门问题