Reputation: 59
I want to call multiple modules using for loop in paintEvent(), like
def fun1(self,paint,a,b):
do something ...
def fun2(self,paint,a,b):
do something ...
def fun3(self,paint,a,b):
do something ....
def paintEvent(self,event=None):
for i in range (1,3):
self.fun[i](self,paint,a,b)
basically my fun1, fun2 and fun3 are drawing some widgets and are almost same. Please help me how to call these functions using for loop.
Upvotes: 0
Views: 229
Reputation: 6796
you could do something like:
def fun1(self, paint, a, b):
pass
def fun2(self, paint, a, b):
pass
def fun3(self,paint, a, b):
do something ....
def paintEvent(self, event=None):
functions = [self.fun1, self.fun2, self.fun3]
for func in functions:
func(paint, a, b)
or if you're sure about the function names, like they have a similar naming pattern, even this could work:
def paintEvent(self, event=None):
for attr_name in dir(self):
if attr_name.startswith('fun'):
func = getattr(self, attr_name)
func(paint, a, b)
Upvotes: 2
Reputation: 3741
You could do something like:
def fun1(self,paint,a,b):
do something ...
def fun2(self,paint,a,b):
do something ...
def fun3(self,paint,a,b):
do something ....
self.fun = [self.fun1,self.fun2,self.fun3]
def paintEvent(self,event=None):
for i in range (1,3):
self.fun[i](self,paint,a,b)
If fun1, fun2, and fun3 are almost the same thing as you have said, you should consider combining them into one function that you can call with an additional argument.
Upvotes: 3