I am trying to create a sub-form to read files within a program. I have multiple buttons which basically repeat the same process on different files.
A button click calls the function "loadfiles" . The parameters passed, using a lambda function, to "loadfiles" are a string and a label.
self.btnPts.clicked.connect(lambda: self.loadfiles("PtsFile",self.label))
self.btnIll.clicked.connect(lambda: self.loadfiles("IllFile",self.label_2))
self.btnSigIll.clicked.connect(lambda: self.loadfiles("SigIll",self.label_3))
self.btn.clicked.connect(lambda: self.loadfiles("FutureFile",self.label_4)
Is there a way that I can group all the buttons together and compress these statements into one statement (something similar to Events in Excel VBA).
Can I add the buttons to a list and then bind the "clicked.connect" to it so that it calls my function evertime.
(I started coding in Python only 2 weeks back and this is my first time here. So please let me know if the details provided in the question are sufficient.)
Update . .
I was able to make this work by using partial functions. As I mentioned in my question above, I wanted to pass a particular string and a related label to a function when a button was clicked. And I wanted to do this in less verbose fashion.
I used a dictionary to store my buttons and used labels as keys. Then I used a for loop for iterating through the dictionary.
buttons = {self.btnPts:self.label,self.btnIll:self.label_2,self.btnSigIll:self.label_3,self.btn:self.label_4}
for button in buttons:
button.clicked.connect(partial(self.loadfiles,button.objectName(),buttons[button]))
Credit: I was able to work my way through this answer.. Connecting slots and signals in PyQt4 in a loop