Enzy
Enzy

Reputation: 185

PyQt5: Find which QPushButton is clicked

I have multiple buttons & want to know which button is clicked. I have found out the error and know that the sender() function has to work with QWidget rather than the class object, but I can't figure out the solution.

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):

    def setupUi(self, MainWindow):
        """Widget code here"""
        self.btn1 = QtWidgets.QPushButton(self.widget)
        """Button properties here"""
        self.btn1.setObjectName("btn1")
        self.btn1.clicked.connect(self.btnListener)

        self.btn2 = QtWidgets.QPushButton(self.widget)
        self.btn2.setObjectName("btn2")
        self.btn2.clicked.connect(self.btnListener)

        """..... more buttons"""

    def btnListener(self):
        sender_button = self.sender() # Error Ui_MainWindow has no attribute sender
        print(sender_button)

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 2

Views: 11582

Answers (5)

Sean
Sean

Reputation: 17

here's a way:

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(QtWidgets.QMainWindow):
    def __init__(self):
        super(Ui_MainWindow, self).__init__()
        self.setupUI()

    def setupUi(self, MainWindow):
        self.MainWindow = MainWindow
        """Widget code here"""
        self.btn1 = QtWidgets.QPushButton(self.widget)
        """Button properties here"""
        self.btn1.setObjectName("btn1")
        self.btn1.clicked.connect(self.btnListener)

        self.btn2 = QtWidgets.QPushButton(self.widget)
        self.btn2.setObjectName("btn2")
        self.btn2.clicked.connect(self.btnListener)

        """..... more buttons"""

    def btnListener(self):
        sender_button = self..sender()
        print(sender_button.text())

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: 0

Riz.Khan
Riz.Khan

Reputation: 493

below code worked well.

import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class DlgMain(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowTitle("Yeh lay")
        self.resize(400,400)

        self.btn1 = QPushButton("btn1", self)
        self.btn1.move(200,200)
        self.btn1.clicked.connect(self.btnListener)

        self.btn2 = QPushButton("btn2", self)
        self.btn2.move(200,230)
        self.btn2.clicked.connect(self.btnListener)

    def btnListener(self):
        rbt = self.sender()
        print(rbt.text())


if __name__ =='__main__':
    app = QApplication(sys.argv)
    dlgMain = DlgMain()
    dlgMain.show()
    sys.exit(app.exec_())

Upvotes: 2

Jean-Charles Naud
Jean-Charles Naud

Reputation: 81

With Qt 4.8 and python2.7, i use partial to pass multi arguments to signal.

from functools import partial
...

def initGui(self):
    ...
    self.btn1.clicked.connect(partial(self.btnListener, "btn1"))
    self.btn2.clicked.connect(partial(self.btnListener, "btn2"))
    ...

def btnListener(self, button_name):
    print('button_name {}'.format(button_name))
    ...

With this method, you can know which button is clicked.

I hope that help you to find the same in QT5.

Upvotes: 8

Enzy
Enzy

Reputation: 185

Done this based on @MrLeeh suggestion & it has worked

from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):

def setupUi(self, MainWindow):
    self.MainWindow = MainWindow
    """Widget code here"""
    self.btn1 = QtWidgets.QPushButton(self.widget)
    """Button properties here"""
    self.btn1.setObjectName("btn1")
    self.btn1.clicked.connect(self.btnListener)

    self.btn2 = QtWidgets.QPushButton(self.widget)
    self.btn2.setObjectName("btn2")
    self.btn2.clicked.connect(self.btnListener)

    """..... more buttons"""

def btnListener(self):
    sender_button = self.MainWindow.sender()
    print(sender_button.text())

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())

Upvotes: -1

MrLeeh
MrLeeh

Reputation: 5589

You need to inherit Ui_MainWindow from MainWindow for this to work.

class Ui_MainWindow(MainWindow):
   ...

Upvotes: 2

Related Questions