通过在 python 中的 for 循环中保存到 var 中的方法名称连接到 Class 方法
Connect to a Class Method by it's method name holded into a var in a for loop in python
我正在将 PyQt4 中的一些按钮连接到同一个函数中,该函数将查找 按钮名称或标题 [=26] 之间的匹配项=] 和 Class 实例方法名称管理不同的操作。
我以不同的方式将 class instance methods
放入列表中,遍历并尝试将其各自的功能连接到其按钮。但我无法以任何方式使用方法callback()
。
##############################################
# Operatios RIG class INSTANCE
#
self.OperationsRig = OperationsFile.Operations()
#### BUTTONS TO CONNECT -----------------------#
mirrorButton.clicked.connect(self.operations_module)
flipButton.clicked.connect(self.operations_module)
resetButton.clicked.connect(self.operations_module)
visibilityButton.clicked.connect(self.operations_module)
def operations_module(self):
# Text gives me the name to Match ------------------#
text = self.sender().text()
# ------------------------#
# have 3 ways of getting the class instance methods into a list to iterate ..
method_list = [func for func in dir(self.OperationsRig) if callable(getattr(self.OperationsRig, func))]
methods = vars(self.OperationsRig.__class__).items()
member =inspect.getmembers(self.OperationsRig, predicate=inspect.ismethod)
# This var is a list to pass to the Method
Sel_list = cmds.ls(sl=True)
# iterate over the "self.OperationsRig" methods
#
""" option 1 """
for meth in method_list:
if text.strip().lower() in meth.lower()[:]:
# if match, and it does, will call the class method and send the data.
pass
#getattr(self.OperationsRig,meth)(Sel_list) # Executes the funcion instance, but is not
# sending the data, I've checked printing inside.
# NoneTypeObject is not iterable. no data sent.
""" option 2 """
for meth in methods:
if text.strip().lower() in meth[0].lower()[:]:
pass
# if match, and it does, will call the class method and send the data.
#method = meth[1] # Gets the memory address of the function. Good.
#method(Sel_list) # Does not return anything, the process of the funcion gets an
# error at the very beggining of the method process
""" option 3 """
for meth in member:
if text.strip().lower() in meth[0].lower()[:]:
meth[1](Sel_list) # Gets the memory address of the function. Good. as a bound method
# Same result as option 1 - # NoneTypeObject is not iterable. no data sent.
# --------------------------------------------------------------------------------------------------#
匹配正常,问题出在函数调用上。我做错了什么?
不知道OperationsRigclass定义,自己定义一个
既然能匹配成功,我想应该不是pyqt的问题。
这是我的代码:
class OperationsRig:
def foo(self, msg):
print('foo:' + str(msg))
return 'foo OK'
def foo1(self, msg):
print('foo1:' + str(msg))
return 'foo1 OK'
opr = OperationsRig()
def operations_module(text):
# Text gives me the name to Match ------------------#
# ------------------------#
# have 3 ways of getting the class instance methods into a list to iterate ..
method_list = [func for func in dir(opr) if callable(getattr(opr, func))]
# This var is a list to pass to the Method
Sel_list = [1,2,3]
# iterate over the "self.OperationsRig" methods
#
""" option 1 """
for meth in method_list:
if text.strip().lower() in meth.lower()[:]:
# if match, and it does, will call the class method and send the data.
pass
return getattr(opr, meth)(Sel_list) # Executes the funcion instance, but is not
# sending the data, I've checked printing inside.
# NoneTypeObject is not iterable. no data sent.
print(operations_module('foo'))
print(operations_module('foo1'))
这是结果:
foo:[1, 2, 3]
foo OK
foo1:[1, 2, 3]
foo1 OK
Process finished with exit code 0
它按我的预期运行,我仍然无法弄清楚为什么你的代码失败了。或者,如果你能给出进一步的解释。
我正在将 PyQt4 中的一些按钮连接到同一个函数中,该函数将查找 按钮名称或标题 [=26] 之间的匹配项=] 和 Class 实例方法名称管理不同的操作。
我以不同的方式将 class instance methods
放入列表中,遍历并尝试将其各自的功能连接到其按钮。但我无法以任何方式使用方法callback()
。
##############################################
# Operatios RIG class INSTANCE
#
self.OperationsRig = OperationsFile.Operations()
#### BUTTONS TO CONNECT -----------------------#
mirrorButton.clicked.connect(self.operations_module)
flipButton.clicked.connect(self.operations_module)
resetButton.clicked.connect(self.operations_module)
visibilityButton.clicked.connect(self.operations_module)
def operations_module(self):
# Text gives me the name to Match ------------------#
text = self.sender().text()
# ------------------------#
# have 3 ways of getting the class instance methods into a list to iterate ..
method_list = [func for func in dir(self.OperationsRig) if callable(getattr(self.OperationsRig, func))]
methods = vars(self.OperationsRig.__class__).items()
member =inspect.getmembers(self.OperationsRig, predicate=inspect.ismethod)
# This var is a list to pass to the Method
Sel_list = cmds.ls(sl=True)
# iterate over the "self.OperationsRig" methods
#
""" option 1 """
for meth in method_list:
if text.strip().lower() in meth.lower()[:]:
# if match, and it does, will call the class method and send the data.
pass
#getattr(self.OperationsRig,meth)(Sel_list) # Executes the funcion instance, but is not
# sending the data, I've checked printing inside.
# NoneTypeObject is not iterable. no data sent.
""" option 2 """
for meth in methods:
if text.strip().lower() in meth[0].lower()[:]:
pass
# if match, and it does, will call the class method and send the data.
#method = meth[1] # Gets the memory address of the function. Good.
#method(Sel_list) # Does not return anything, the process of the funcion gets an
# error at the very beggining of the method process
""" option 3 """
for meth in member:
if text.strip().lower() in meth[0].lower()[:]:
meth[1](Sel_list) # Gets the memory address of the function. Good. as a bound method
# Same result as option 1 - # NoneTypeObject is not iterable. no data sent.
# --------------------------------------------------------------------------------------------------#
匹配正常,问题出在函数调用上。我做错了什么?
不知道OperationsRigclass定义,自己定义一个
既然能匹配成功,我想应该不是pyqt的问题。
这是我的代码:
class OperationsRig:
def foo(self, msg):
print('foo:' + str(msg))
return 'foo OK'
def foo1(self, msg):
print('foo1:' + str(msg))
return 'foo1 OK'
opr = OperationsRig()
def operations_module(text):
# Text gives me the name to Match ------------------#
# ------------------------#
# have 3 ways of getting the class instance methods into a list to iterate ..
method_list = [func for func in dir(opr) if callable(getattr(opr, func))]
# This var is a list to pass to the Method
Sel_list = [1,2,3]
# iterate over the "self.OperationsRig" methods
#
""" option 1 """
for meth in method_list:
if text.strip().lower() in meth.lower()[:]:
# if match, and it does, will call the class method and send the data.
pass
return getattr(opr, meth)(Sel_list) # Executes the funcion instance, but is not
# sending the data, I've checked printing inside.
# NoneTypeObject is not iterable. no data sent.
print(operations_module('foo'))
print(operations_module('foo1'))
这是结果:
foo:[1, 2, 3]
foo OK
foo1:[1, 2, 3]
foo1 OK
Process finished with exit code 0
它按我的预期运行,我仍然无法弄清楚为什么你的代码失败了。或者,如果你能给出进一步的解释。