nlgootee nlgootee - 1 month ago 7
Python Question

Unable to send signal from Button created in Python loop

I am trying to create a series of buttons in a loop. Each button gets an id number as it's text and when the button is clicked it is supposed to send the id number to a function that will open an archived order. At this time I just want to print the order number to prove that the signal works and each button is connected to the correct order number.

ui.cmdOpen = QtWidgets.QPushButton(ui.frOrdHist)
ui.cmdOpen.setGeometry(QtCore.QRect(269, line1Y, 61, 22))
ui.cmdOpen.setText(iOrderId)
ui.cmdOpen.setObjectName("cmdOpen")
ui.cmdOpen.clicked.connect(lambda button=ui.cmdOpen:displayOrder(ui, button))


def displayOrder(ui, button):
i = button.text()
print(i)


When I click the button, I get an error message that says "boolean object has no text attribute"

I tried passing the order number directly and it would print "False" so still a boolean. I don't know where the boolean is coming from, it must be something wrong in the signal.

Answer

The clicked signal always sends the checked-state of the button. This will overwrite your button argument with a boolean value, which is why you get the atribute-error. So you should make the connection like this:

ui.cmdOpen.clicked.connect(
    lambda checked, button=ui.cmdOpen: displayOrder(ui, button))