How to modify the called function when I click on a QPushButton, according to a QRadioButton?

qpushbutton clicked
qpushbutton change text
qt connect(button to function)
qpushbutton disable
qpushbutton animate click
qpushbutton pyside
qpushbutton disable python
qpushbutton size python

I have three QRadioButtons. Only one can be selected at a time. I have a QPushButton, which must call a different function, according to the selected QRadioButton.

Example: If is selected the QRadioButton #1, the button executes the function #1 when is clicked. If is selected the QRadioButton #2, the same button executes the function #2, and so on.


Make your choice. Try it:

import sys
from PyQt5.QtWidgets import (QLabel, QRadioButton, QPushButton, QVBoxLayout, QApplication, QWidget)


class Window(QWidget):

    def __init__(self):
        super().__init__()

        self.init_ui()

    def init_ui(self):
        self.lbl  = QLabel('Which do you like best?')
        self.code = QRadioButton('PyQt5')
        self.dog  = QRadioButton('Dogs')
        self.cat  = QRadioButton('Cats')
        self.btn  = QPushButton('Select')

        layout = QVBoxLayout()
        layout.addWidget(self.lbl)
        layout.addWidget(self.code)
        layout.addWidget(self.dog)
        layout.addWidget(self.cat)
        layout.addWidget(self.btn)

        self.setLayout(layout)
        self.setWindowTitle('PyQt5 QRadioButton')

        self.btn.clicked.connect(lambda: self.btn_clk(self.code.isChecked(), self.lbl))
        self.btn.clicked.connect(lambda: self.btn_clk(self.dog.isChecked(), self.lbl))
        self.btn.clicked.connect(lambda: self.btn_clk(self.cat.isChecked(), self.lbl))

        self.show()

    def btn_clk(self, chk, lbl):
        if self.code.isChecked():
            lbl.setText('It`s a good choice')
        elif self.dog.isChecked():
            lbl.setText('I guess you like dogs')
        else:
            lbl.setText('So its cats for you')

app = QApplication(sys.argv)
a_window = Window()
sys.exit(app.exec_())

QPushButton Class, The QPushButton widget provides a command button. A push button emits the signal clicked() when it is activated by the mouse, the You can change this with setAutoDefault(). If you do not want this space around your buttons, call setAutoDefault(false). Use the setMinimumSize() function to prevent this behavior. A QRadioButton is an option button that can be switched on (checked) or off (unchecked). Radio buttons typically present the user with a "one of many" choice. In a group of radio buttons, only one radio button at a time can be checked; if the user selects another button, the previously selected button is switched off.


I had to changed it a little bit for my code, and it worked. Thank you!

...
class Window(QWidget):
    def __init__(self):
    ...
    def init_ui(self):
        ...    
        self.bnt.clicked.connect(lambda: self.decision_maker())
        ...

    def decision_maker(self):
        if self.radiobutton1.isChecked():
            do_something_1()
        elif self.radiobutton2.isChecked():
            do_something_2()
        ...

QRadioButton Class, The QRadioButton widget provides a radio button with a text label. Reimplemented Protected Functions Just like QPushButton, a radio button displays text, and optionally a small icon. setText(), text(), setDown(), isDown(), autoRepeat(), group(), setAutoRepeat(), toggle(), pressed(), released(), clicked(), and toggled(). I have three QRadioButtons. Only one can be selected at a time. I have a QPushButton, which must call a different function, according to the selected QRadioButton. Example: If is selected the QRadioButton #1, the button executes the function #1 when is clicked. If is selected the QRadioButton #2, the same button executes the function #2, and so on.


Try this:

from PyQt5 import QtGui,QtCore,QtWidgets
import sys
class Win(QtWidgets.QWidget):
   def __init__(self):
      super().__init__()
      self.initUI()
   def initUI(self):
      self.vbox = QtWidgets.QVBoxLayout(self)
      self.rb1 = QtWidgets.QRadioButton('1',self)
      self.rb2 = QtWidgets.QRadioButton('2',self)
      self.rb3 = QtWidgets.QRadioButton('3',self)
      self.btn = QtWidgets.QPushButton('PushMe',self)
      self.btn.clicked.connect(self.func)
      self.vbox.addWidget(self.rb1)
      self.vbox.addWidget(self.rb2)
      self.vbox.addWidget(self.rb3)
      self.vbox.addWidget(self.btn)
      self.show()

   def func(self):
      for i in self.findChildren(QtWidgets.QRadioButton):
         if i.isChecked():
            self.doAction(str(i.text()))

   def doAction(self,text):
      if text == '1':
         print('You like to be the first')
      if text == '2':
         print('You like to be the second')
      if text == '3':
         print('You like to be the third')
if __name__ == '__main__':
   app = QtWidgets.QApplication(sys.argv)
   gui = Win()
   sys.exit(app.exec_())

PyQt5 – Change text in Radio Button on click, In this article we will see how to change the text in the radio button. even we can leave it blank, radio button is created with the help of QRadioButton class. push button, such that when push button get pressed this method should get called. In the method change the radio button content with the help of setText method. void QButtonGroup::moveFocus ( int key ) [virtual] Moves the keyboard focus according to key, and if appropriate checks the new focus item. This function does nothing unless the keyboard focus points to one of the button group members and key is one of Key_Up, Key_Down, Key_Left and Key_Right. void QButtonGroup::pressed ( int id ) [signal]


Foundations of Qt Development, The paintEvent method is called by the event loop when a widget needs to be redrawn. see how a button is modified to look different. This class is then inherited into QPushButton, QRadioButton, and QCheckBox, which implement three  Performs an animated click: the button is pressed immediately, and released msec milliseconds later (the default is 100 ms). Calling this function again before the button was released will reset the release timer. All signals associated with a click are emitted as appropriate. This function does nothing if the button is disabled. See also click().


Beginning Linux?Programming, QPushButton is the standard generic button that contains text such as “Ok” or Like all QButtons, it emits the clicked signal when it's activated, and we'll into a toggle button (i.e., can be turned on or off) by calling setToggleButton. QCheckBox can also be set by us to be a third, in-between state that indicates “​no change. i am trying to change the color of radiobutton on selected and deselected as QtStylesheet :Qt Stylesheet. but In this link it only refer to Loading a Image but how could I change it color and without loading Image and change border color or styling radiobutton . the requirement is attached in the Image : RadioButtonRequiredImage. Thanks for support


Programming with Qt: Writing Portable GUI applications on Unix and , This is done by passing the event filter Object to the method QApplication: The occasions when you really need a fake event (also called a synthetic event) are  Works fine so far. The only Problem is that i have to click multiple times to rotate the object a little further. This is a bit annoying. Isnt there a possibility that the button will stay pressed as long as I press it and the object will rotate further. There is the function pressed(), but there is no difference to clicked().