Python Forum
[PyQt] how to deselect radiobutton in pyqt5
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] how to deselect radiobutton in pyqt5
#1
Hello;
I created radiobuttons (radioButton_degre, radioButton_ddm), checkBox(checkbox_confirmed) and a button "Reset" (pushButton_reset) with Qt Designer

when I click on the button, I want all radiobouttons and checkbox to become unselected.
the fonctions display_degre_unit and display_ddm_unit are not defined, to reduce the code for a simplified code

#!/usr/bin/python3
# -*- coding: utf-8 -*-

# PyQT5 :
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *


class MainApp(QMainWindow,FORM_CLASS) :
    def __init__(self,parent=None) :
        super(MainApp,self).__init__(parent)
        QMainWindow.__init__(self)
        self.setupUi(self)
		
		self.pushButton_reset.clicked.connect(self.initialize)
        self.radioButton_degre.toggled.connect(self.display_degre_unit)
        self.radioButton_ddm.toggled.connect(self.display_ddm_unit)

	def initialize(self):

		self.checkbox_confirmed.setCheckState(Qt.Unchecked)
		self.radioButton_degre.setChecked(False)
		self.radioButton_ddm.setChecked(False)
		

def main():
    app=QApplication(sys.argv)
    window=MainApp()
    window.show()
    app.exec_()

if __name__=='__main__' :
    main()
It works for the checkbox but does not work for radiobuttons

thanks for help
Reply
#2
Okay well because you appear to be using the Designer [for some unholy reason ;)] this is not an MRE as you cannot run it and determine what is going on -- however you could have created a simple MRE by doing things properly (aka not using that horrible designer code) as follows -- also I have included the fix to your issue which I got by doing a simple google search for pyqt QRadioButton setChecked false not working
from sys import exit as sysExit

from PyQt5.QtCore    import *
from PyQt5.QtGui     import *
from PyQt5.QtWidgets import *
  
class MainApp(QWidget) :
    def __init__(self,parent=None) :
# Do not use 'Super( )' unless you fully understand what it is for
# as its benefit will actually occur less often than its pitfalls    
        QWidget.__init__(self)

      # Removing this bit of useless code
      #  self.setupUi(self)
      
        self.pushButton_reset = QPushButton('Reset')
        self.pushButton_reset.clicked.connect(self.initialize)

        self.radioButton_degre = QRadioButton('Degrees')
        self.radioButton_degre.toggled.connect(self.display_degre_unit)

        self.radioButton_ddm = QRadioButton('DDM Units')
        self.radioButton_ddm.toggled.connect(self.display_ddm_unit)

        self.RadioGroup = QButtonGroup()
        self.RadioGroup.addButton(self.radioButton_degre)
        self.RadioGroup.addButton(self.radioButton_ddm)       

        self.checkbox_confirmed = QCheckBox('Confirmed')
        
        HBox = QHBoxLayout()
        HBox.addWidget(self.pushButton_reset)
        HBox.addWidget(QLabel('     '))  # Simple horizontal spacer
        HBox.addWidget(self.radioButton_degre)
        HBox.addWidget(self.radioButton_ddm)
        HBox.addWidget(QLabel('   '))  # Simple horizontal spacer
        HBox.addWidget(self.checkbox_confirmed)
        HBox.addStretch(1)
        
        VBox = QVBoxLayout()
        VBox.addLayout(HBox)
        VBox.addStretch(1)
        
        self.setLayout(VBox)
 
    def initialize(self):
        self.RadioGroup.setExclusive(False)

        self.radioButton_degre.setChecked(False)
        self.radioButton_ddm.setChecked(False)

        self.RadioGroup.setExclusive(True)

        self.checkbox_confirmed.setCheckState(Qt.Unchecked)

    def display_degre_unit(self):
        pass

    def display_ddm_unit(self):
        pass

if __name__=='__main__' :
    app=QApplication([])
    window=MainApp()
    window.show()
    sysExit(app.exec_())
Reply
#3
to solve the problem; I proceeded as follows: I group my radiobuttons in a QButtonGroup into Qt Designer, like that :

Quote:"Select the ones you want to group, and right click on one of them. Then, you'll find an option to "group" the buttons. If you click that, you will notice that a new QButtonGroup has appeared in your object browser in designer. You can select that button group to give it a name, make it exclusive"

Then, I integrated the following code in my function ;

self.checkbox_confirmed .setCheckState(Qt.Unchecked)
self.buttonGroup_unite_mesure.setExclusive(False)
self.radioButton_degre.setChecked(False)
self.radioButton_ddm.setChecked(False)

self.buttonGroup_unite_mesure.setExclusive(True)
thanks for the previous code posted by Denni, which gave me an idea how to solve my problem
Reply
#4
Well you can continue to use the Designer and enjoy having continuous headaches down the road it is your choice -- however I have taught numerous Designer coders how to create a normal PyQt program and everyone of them (some slower than others) have come to realize just how much of a headache it is to use the Designer over coding PyQt as it was meant to be used. So I am fine with you beating your head on that proverbial brick wall but if you should ever get tired of giving yourself a headache just shoot me message (somehow) and I will be more than happy to help you learn the proper way to code PyQt --- by the way I think the Designer was actually meant to be used to make Web Pages not PyQt python code -- so unless your creating web pages you are definitely using a tool meant for some other purpose.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to dynamically change radiobutton text kenwatts275 2 1,202 Mar-05-2021, 02:25 AM
Last Post: deanhystad
  Python3 tkinter radiobutton problem Nick_tkinter 14 1,827 Feb-15-2021, 11:01 PM
Last Post: Nick_tkinter
  [Tkinter] RadioButton Maryan 2 801 Oct-23-2020, 09:36 PM
Last Post: Maryan
  [Tkinter] How to create radiobutton in numpy gui python? luthfidali 2 1,283 May-23-2020, 10:35 AM
Last Post: timo
  Tkinter: increasing numbers and Radiobutton issue PeroPuri 1 898 Apr-13-2020, 05:48 PM
Last Post: deanhystad
  Huge code problems (buttons(PyQt5),PyQt5 Threads, Windows etc) ZenWoR 0 1,611 Apr-06-2019, 11:15 PM
Last Post: ZenWoR
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 3,132 Feb-02-2019, 01:53 AM
Last Post: woooee
  [Tkinter] Radiobutton in Tkinter not Updating Variable from Value Aidan54321 1 5,469 Jun-16-2017, 11:08 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020