我想自动生成按钮连接.但不要工作:
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应该在函数button[i]处绑定_j ( def _1(self): / def _2(self): / ...,但在执行时绑定:
connect() slot argument should be a callable or a signal, not 'str'怎么修呢?
发布于 2014-01-18 19:00:10
错误信息说明这一切,您需要传递一个函数或信号。尝试使用getattr获取字符串所表示的函数。
像这样的东西可能会起作用
self.button[i].clicked.connect(getattr(self, '_'.format(j)))发布于 2014-01-18 19:00:09
尝试创建一个实际的可调用方法,而不是将字符串发送到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)( make_slot函数是为了防止python后期绑定的怪癖,请参阅Creating functions in a loop )
另外,也许您可以直接将插槽添加到self中,而不是构建一个dict:
setattr(self, 'on_button_%i_clicked' % i, slot) # perhaps more convenient?这样,如果需要的话,可以单独将它们称为self.on_button_42_clicked。
https://stackoverflow.com/questions/21208436
复制相似问题