Question

I want to generate automatically the button-connections... but dont work:

    self._ = {}
    j = 0
    for i in self.btn:
        self._[i] = 'self._' + repr(j)
        print self._[i]
        self.button[i].clicked.connect(self._[i])
        j += 1

should bind the button[i] at the function _j ( def _1(self): / def _2(self): / ... but at execute:

connect() slot argument should be a callable or a signal, not 'str'

how to fix it?

Was it helpful?

Solution

The error message says it all, you need to pass a function or signal. Try to use getattrto get the function the string is representing.

Something like this may work

self.button[i].clicked.connect(getattr(self, '_'.format(j)))

OTHER TIPS

Try creating an actual callable method instead of sending the string to connect():

def make_slot(self, i):
    print 'clicked %i' % i 

self._ = {}
for i in self.btn:
    slot = self.make_slot(i)
    self._[i] = slot
    self.button[i].clicked.connect(slot)

(The make_slot function is to prevent python late-binding quirkiness, see Creating functions in a loop about that)

Also, instead of building a dict, maybe you could add the slots directly to self:

setattr(self, 'on_button_%i_clicked' % i, slot) # perhaps more convenient?

That way, you could refer to them separately as self.on_button_42_clicked if you need too.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top