0

I have two functions, two radio buttons, and a pushbutton.

def A():
    pass
def B():
    pass

How can I run the Function A if radioButton A is selected and Function B if radioButton B is selected when pushing on the pushButton? I've tried something like

if dlg.A_radioButton.clicked:
    dlg.calculate_pushButton.clicked.connect(A)
elif dlg.B_radioButton.clicked:
    dlg.calculate_pushButton.clicked.connect(B)

1 Answers1

0

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 ?')
        self.rb1 = QRadioButton('PyQt4')
        self.rb2 = QRadioButton('PyQt5')
        self.rb2.setChecked(True)
        self.btn = QPushButton('Select')

        layout = QVBoxLayout()
        layout.addWidget(self.lbl)
        layout.addWidget(self.rb1)
        layout.addWidget(self.rb2)
        layout.addWidget(self.btn)

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

        self.btn.clicked.connect(lambda: self.btn_clk(self.rb1.isChecked(), self.lbl))

    def btn_clk(self, chk, lbl):
        if chk:
            lbl.setText('It1s time to switch to PyQt5.')
            self.A('It1s time to switch to PyQt5.')
        else:
            lbl.setText('It`s a great choice!')
            self.B('It`s a great choice!')

    def A(self, text):
        print(text)

    def B(self, text):
        print(text)

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

enter image description here

S. Nick
  • 12,879
  • 8
  • 25
  • 33