Search code examples
pythonpyqtpyqt4qpushbuttonqradiobutton

Automatically enabling different QPushbuttons according to QRadioButton options in PyQt


I am very new to PyQt, so I am not even sure where to start searching for this.

So I have two different options for QRadioButtons which ideally will correspond to two QPushButtons, one each.

Basically, I have the following code, where i tried to achieve this by using if statements:

def tab1UI(self): 

    mytabfont = QFont('Lucida Sans Unicode', 9)    
    layout = QFormLayout()
    #self.setTabText(0,"My Data")
    self.tab1.setLayout(layout)

    tabdescription = 'To obtain or generate data choose an option below:' 
    # radio options



    label1 = QLabel(tabdescription)
    label1.setFont(mytabfont)
    layout.addWidget(label1)

    radiobtn1 = QRadioButton('Load data from file')
    radiobtn1.setChecked(True)

    #why does my resize not work?
    radiobtn1.resize(100,100)

    radiobtn1.setFont(mytabfont)
    layout.addWidget(radiobtn1)
    loadbtn = QPushButton('Open CSV file...')
    layout.addWidget(loadbtn)

    radiobtn2 = QRadioButton('Generate data')
    radiobtn2.setFont(mytabfont)
    genbtn= QPushButton('Generating matrix...')
    layout.addWidget(radiobtn2)
    layout.addWidget(genbtn)



    if radiobtn1.isChecked():

        # if this option is clicked then this button needs to be activated else it must be de-activated
        loadbtn.setEnabled(True)
        genbtn.setEnabled(False)

    elif radiobtn2.isChecked():

        loadbtn.setEnabled(False)
        genbtn.setEnabled(True)

    else: 

        loadbtn.setEnabled(False)
        genbtn.setEnabled(False)

So, whenever I click one radio-button option I would like one pushbutton to become automatically active or inactive when the other option is checked instead. There must be some sort of Action to be connected but not sure how to go about this.


Solution

  • You're only running the if statement once, when the buttons are first created. In order for this to work, you need to evaluate those if statements every time the radio button check state is changed. Qt allows you to do this with Signals and Slots. The QRadioButton will emit a signal when you change the check state. You can connect to this signal and run a function that updates the enabled state of the other buttons.

    def tab1UI(self): 
        mytabfont = QFont('Lucida Sans Unicode', 9)    
        layout = QFormLayout()
        self.tab1.setLayout(layout)
    
        tabdescription = 'To obtain or generate data choose an option below:' 
        # radio options
        self.label1 = QLabel(tabdescription)
        self.label1.setFont(mytabfont)
        layout.addWidget(self.label1)
    
        self.radiobtn1 = QRadioButton('Load data from file')
        self.radiobtn1.setChecked(True)
    
        self.radiobtn1.setFont(mytabfont)
        layout.addWidget(self.radiobtn1)
        self.loadbtn = QPushButton('Open CSV file...')
        layout.addWidget(self.loadbtn)
    
        self.radiobtn2 = QRadioButton('Generate data')
        self.radiobtn2.setFont(mytabfont)
        self.genbtn= QPushButton('Generating matrix...')
        layout.addWidget(self.radiobtn2)
        layout.addWidget(self.genbtn)
    
        self.radiobtn1.toggled.connect(self.refresh_button_state)
        self.radiobtn2.toggled.connect(self.refresh_button_state)
    
        self.refresh_button_state()
    
    
    def refresh_button_state(self):
        if self.radiobtn1.isChecked():
            self.loadbtn.setEnabled(True)
            self.genbtn.setEnabled(False)
        elif self.radiobtn2.isChecked():
            self.loadbtn.setEnabled(False)
            self.genbtn.setEnabled(True)
        else: 
            self.loadbtn.setEnabled(False)
            self.genbtn.setEnabled(False)