Rifqi Khairur Rahman Rifqi Khairur Rahman - 1 year ago 199
Python Question

pyQt pushButton connect creation within loop

I am new to pyqt (pyside in maya), please be nice to me :)

so the point is i want to generate many button via loop statement, something simmilar like below :

(5 button in a window - complete code - maya)

import PySide.QtCore as qc
import PySide.QtGui as qg
class simpleUI(qg.QDialog):
def __init__(self):
self.setWindowTitle('Simple UI')
for x in range(5) :
self.btn[x]= qg.QPushButton(self)
self.btn[x].setText('this is btn number{0}'.format(x))
self.btn[x].setGeometry(qc.QRect(0,100+(x*20), 100,20))
self.btn[x].clicked.connect(lambda : self.notifyMe(x))
def notifyMe(self,index):
print index
dialog = simpleUI()

as you see, I store button object in array, but the problem is when i want to connect btn signal with notifyMe function, every button always give x same value (x=4) , meanwhile in button.setText x succeed give unique incremental value, i can't figure it why..

Answer Source

It is old problem with function in lambda - it doesn't get value from x when you declare function but when you click button. But when you click button then for-loop is over and x keeps last value - and this way all buttons use the same value. You have to use

lambda a=x: self.notifyMe(a)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download