【发布时间】:2018-05-30 12:32:43
【问题描述】:
我有这样的代码。在我的原始代码中,我想在单击按钮时调用两个或多个方法。
class MyApp(object):
@staticmethod
def combine_funcs(*funcs):
def combined_func(*args, **kwargs):
for f in funcs:
f(*args, **kwargs)
return combined_func
def __init__(self):
MyApp.combine_funcs(self.my_first_func, self.my_second_func)
def my_first_func(self):
print("First")
def my_second_func(self):
print("Second")
my_app = MyApp()
当我运行它时,什么都没有打印出来。为什么?
【问题讨论】:
-
您只是在组合函数,而不是调用新组合的函数函数。
-
好的,谢谢。我需要括号。但是,当我想在按下按钮时调用它们时
self.ui.pushButton.clicked.connect(MyApp.combine_funcs(self.converter(), self.showMessage()))是一样的吗?它们会立即被调用,而不是等待鼠标点击? -
combine_funcs(self.converter(), ...)和combine_funcs(self.my_first_func, ...)不一样,现在是吗? -
好的,我的代码似乎有更多问题。
()执行一个函数。这就是为什么我的 init 什么也没做。我现在明白了。第二个问题是当我在self.ui.pushButton.clicked.connect(MyApp.combine_funcs(self.converter, self.showMessage))中删除()时,我收到错误converter() takes 1 positional argument but 2 were given。转换器函数只接受self参数。我要发送哪两个参数? -
当它最终被调用时,它正在被传递参数......
标签: python static-classes