Typically, most widgets are created in the installation code for the main window. It is a good idea to always add this widget as attributes of the main window so that you can easily access them later:
class MainWindow(QtGui.QMainWindow): def __init__(self, parent=None) super(MainWindow, self).__init__(parent) ... self.button = QtGui.QPushButton("start go") self.button.clicked.connect(self.buttonClick) ... def buttonClick(self): print(self.button.text())
If you have many buttons that use the same handler, you can add buttons to the QButtonGroup and connect the handler to the buttonClicked signal. This signal can be sent either by a pressed button or by an identifier that you specified yourself.
It is also possible to use self.sender() to get a reference to the object that sent the signal. However, sometimes this is considered bad practice because it undermines the main reason for using signals in the first place (see Warnings in the docs for the sender for more on this).
source share