4

I am new to PyQT5 and I want to have several buttons and have the one clicked last in a "checked" state. When another button is clicked the previous one gets "unchecked" while the clicked one gets "checked".

import sys
from PyQt5.QtWidgets import *

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(30, 30, 400, 200)
        self.initUI()

    def initUI(self):
        self.button1 = QPushButton(self)
        self.button1.setGeometry(40, 40, 100, 50)
        self.button1.setText("Button 1")

        self.button2 = QPushButton(self)
        self.button2.setGeometry(150, 40, 100, 50)
        self.button2.setText("Button 2")

        self.show()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

3 Answers 3

7

You can use functools partial or the sender method to check which button was pressed:

import sys
from PyQt5.QtWidgets import *
from functools import partial
class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(30, 30, 400, 200)
        self.initUI()

    def initUI(self):
        self.button1 = QPushButton(self)
        self.button1.setGeometry(40, 40, 100, 50)
        self.button1.setText("Button 1")
        self.button1.clicked.connect(partial(self.clicked_btn, 'Button 1'))

        self.button2 = QPushButton(self)
        self.button2.setGeometry(150, 40, 100, 50)
        self.button2.setText("Button 2")
        self.button2.clicked.connect(partial(self.clicked_btn, 'Button 2'))

        self.show()

    def clicked_btn(self, value):
        print(f'{value} clicked')
        sender = self.sender()
        print(f'Sender says: {sender.text()} was clicked')

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())
1
  • this is an amazing solution as well. People need to vote this up!
    – Ice Bear
    Commented Sep 10, 2021 at 8:02
6

Building off of your code, you can add button1 and button2 to a QButtonGroup with the exclusive property set to True.

class Example(QWidget):
    def __init__(self):
        super().__init__()
        self.setGeometry(30, 30, 400, 200)
        self.initUI()

    def initUI(self):
        self.button1 = QPushButton(self)
        self.button1.setGeometry(40, 40, 100, 50)
        self.button1.setText("Button 1")

        self.button2 = QPushButton(self)
        self.button2.setGeometry(150, 40, 100, 50)
        self.button2.setText("Button 2")

        self.btn_grp = QButtonGroup()
        self.btn_grp.setExclusive(True)
        self.btn_grp.addButton(self.button1)
        self.btn_grp.addButton(self.button2)

        self.btn_grp.buttonClicked.connect(self.on_click)

        self.show()

    def on_click(self, btn):
        pass # do something with the button clicked

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())

Here I've also connected an empty slot to the signal QButtonGroup.buttonClicked, which is emitted whenever a button from the group is clicked.

To find out which button is the currently checked button, you can invoke the methods QButtonGroup.checkedButton() and QButtonGroup.checkedId(). The former will return a QButton object and the latter will return an index int, corresponding to the order in which the buttons were added to the group.

2
  • how do you call 'QButtonGroup.checkedButton()' from inside of "on_click" method? You are passing there QPushButton, so you can ask for btn.text() but how you check group from button itself ?
    – tmdag
    Commented Apr 14, 2019 at 16:52
  • Good answer. It's worth mentioning that according to docs, setExclusive property defaults to True
    – bfris
    Commented Apr 24, 2020 at 16:56
0
  1. Make the buttons checkable by setCheckable(True)
  2. Add the buttons to a QButtonGroup() and the rest is sorted automatically:

    class Example(QWidget):
       def __init__(self):
           super().__init__()
           self.setGeometry(30, 30, 400, 200)
           self.initUI()
    
       def initUI(self):
           self.button1 = QPushButton(self)
           self.button1.setGeometry(40, 40, 100, 50)
           self.button1.setText("Button 1")
           self.button1.setCheckable(True)
           self.button2 = QPushButton(self)
           self.button2.setGeometry(150, 40, 100, 50)
           self.button2.setText("Button 2")
           self.button2.setCheckable(True)
    
           self.my_button_group = QButtonGroup()
           self.my_button_group.addButton(self.button1)
           self.my_button_group.addButton(self.button2)
    
           self.show()
    
1
  • Actually you are correct setting buttons to get checked while clicked will be used to understand which button is currently pushed by the user.
    – graj499
    Commented Jun 2, 2021 at 3:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.