Answer the question
In order to leave comments, you need to log in
How to reset Radiobutton in PyQt5?
Good afternoon everyone! Faced such a problem: the user selects some option from a group of Radiobuttons, after which they are hidden (.hide()) and then after some time they are shown again (.show()), the user must again select some then a button from this group, but its previous selection should be reset. I tried iterating and setting .setChecked(False), then they are really reset, but this happens when displaying, that is, when drawing, you can see how the burning flag of the button goes out for a fraction of a second. What can be done so that they are shown a second time already reset to zero?
Answer the question
In order to leave comments, you need to log in
There was something incomprehensible with your code: the buttons were pressed somehow strangely, the radio buttons were not in the group. In general, it was easier for me to reproduce the look of the window in QTDesigner'e and then copy the generated Python code directly into your former one, making it a class. Well, then add a call btn.setAutoExclusive(False/True) - to reset the button. And so that it doesn’t blink when shown, add repaint () at Hide. See below:
import sys
from PyQt5 import QtWidgets, QtCore
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(196, 153)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.groupBox = QtWidgets.QGroupBox(self.centralwidget)
self.groupBox.setGeometry(QtCore.QRect(10, 10, 81, 111))
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Minimum, QtWidgets.QSizePolicy.Minimum)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.groupBox.sizePolicy().hasHeightForWidth())
self.groupBox.setSizePolicy(sizePolicy)
self.groupBox.setTitle("")
self.verticalLayoutWidget = QtWidgets.QWidget(self.groupBox)
self.verticalLayoutWidget.setGeometry(QtCore.QRect(10, 10, 61, 91))
self.verticalLayout = QtWidgets.QVBoxLayout(self.verticalLayoutWidget)
self.verticalLayout.setSizeConstraint(QtWidgets.QLayout.SetFixedSize)
self.verticalLayout.setContentsMargins(0, 0, 0, 0)
self.radio_button_1 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.verticalLayout.addWidget(self.radio_button_1)
self.radio_button_2 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.verticalLayout.addWidget(self.radio_button_2)
self.radio_button_3 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.verticalLayout.addWidget(self.radio_button_3)
self.radio_button_4 = QtWidgets.QRadioButton(self.verticalLayoutWidget)
self.verticalLayout.addWidget(self.radio_button_4)
self.hide_button = QtWidgets.QPushButton(self.centralwidget)
self.hide_button.setGeometry(QtCore.QRect(100, 20, 75, 23))
self.show_button = QtWidgets.QPushButton(self.centralwidget)
self.show_button.setGeometry(QtCore.QRect(100, 50, 75, 23))
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.radio_button_1.setText(_translate("MainWindow", "One"))
self.radio_button_2.setText(_translate("MainWindow", "Two"))
self.radio_button_3.setText(_translate("MainWindow", "Three"))
self.radio_button_4.setText(_translate("MainWindow", "Four"))
self.hide_button.setText(_translate("MainWindow", "Hide"))
self.show_button.setText(_translate("MainWindow", "Show"))
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(MyWindow, self).__init__()
self.setupUi(self)
self.hide_button.clicked.connect(self.hide_radio_buttons)
self.show_button.clicked.connect(self.show_radio_buttons)
def hide_radio_buttons(self):
for btn in [self.radio_button_1, self.radio_button_2, self.radio_button_3, self.radio_button_4]:
btn.setAutoExclusive(False)
btn.setChecked(False)
btn.repaint()
btn.setAutoExclusive(True)
btn.hide()
def show_radio_buttons(self):
for btn in [self.radio_button_1, self.radio_button_2, self.radio_button_3, self.radio_button_4]:
btn.show()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question