Answer the question
In order to leave comments, you need to log in
PyQt5 QButtonGroup() how to pass pressed button name to function?
I am writing invincible tic-tac-toe, here is a piece of code with a problem:
self.btn_grp = QButtonGroup()
self.btn_grp.setExclusive(True)
self.btn_grp.addButton(self.pushButton_6, id = 6)
self.btn_grp.addButton(self.pushButton_7, id = 7)
self.btn_grp.addButton(self.pushButton_2, id = 2)
self.btn_grp.addButton(self.pushButton_1, id = 1)
self.btn_grp.addButton(self.pushButton_5, id = 5)
self.btn_grp.addButton(self.pushButton_9, id = 9)
self.btn_grp.addButton(self.pushButton_8, id = 8)
self.btn_grp.addButton(self.pushButton_3, id = 3)
self.btn_grp.addButton(self.pushButton_4, id = 4)
self.btn_grp.checkedId()
self.btn_grp.buttonClicked.connect(self.pressed())
Answer the question
In order to leave comments, you need to log in
Qt has a special sender() function that returns the object that was clicked.
Call it and get the button name through it.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question