samedi 17 avril 2021

How to uncheck all checkboxes when one checkbox is checked in PyQt5? [duplicate]

There are 04 checkboxes. When a user checks one checkbox I want the other checkboxes to uncheck (if the user might have checked multiple ones) so that the checkbox the user checked will only remain checked. I tried to do this manually but it's not giving me what I have mentioned above. Any suggestions?

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *


class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(800, 600)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.label = QtWidgets.QLabel(self.centralwidget)
        self.label.setGeometry(QtCore.QRect(20, 30, 211, 131))
        font = QtGui.QFont()
        font.setFamily("Myanmar Text")
        font.setPointSize(28)
        self.label.setFont(font)
        self.label.setObjectName("label")
        self.label_2 = QtWidgets.QLabel(self.centralwidget)
        self.label_2.setGeometry(QtCore.QRect(20, 120, 211, 131))
        font = QtGui.QFont()
        font.setFamily("Myanmar Text")
        font.setPointSize(28)
        self.label_2.setFont(font)
        self.label_2.setObjectName("label_2")
        self.label_3 = QtWidgets.QLabel(self.centralwidget)
        self.label_3.setGeometry(QtCore.QRect(20, 210, 211, 131))
        font = QtGui.QFont()
        font.setFamily("Myanmar Text")
        font.setPointSize(28)
        self.label_3.setFont(font)
        self.label_3.setObjectName("label_3")
        self.label_4 = QtWidgets.QLabel(self.centralwidget)
        self.label_4.setGeometry(QtCore.QRect(20, 310, 211, 131))
        font = QtGui.QFont()
        font.setFamily("Myanmar Text")
        font.setPointSize(28)
        self.label_4.setFont(font)
        self.label_4.setObjectName("label_4")
        self.checkBox = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox.setGeometry(QtCore.QRect(250, 50, 171, 81))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.checkBox.setFont(font)
        self.checkBox.setText("")
        self.checkBox.setObjectName("checkBox")
        self.checkBox_2 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_2.setGeometry(QtCore.QRect(250, 140, 171, 81))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.checkBox_2.setFont(font)
        self.checkBox_2.setText("")
        self.checkBox_2.setObjectName("checkBox_2")
        self.checkBox_3 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_3.setGeometry(QtCore.QRect(250, 230, 171, 81))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.checkBox_3.setFont(font)
        self.checkBox_3.setText("")
        self.checkBox_3.setObjectName("checkBox_3")
        self.checkBox_4 = QtWidgets.QCheckBox(self.centralwidget)
        self.checkBox_4.setGeometry(QtCore.QRect(250, 330, 171, 81))
        font = QtGui.QFont()
        font.setPointSize(16)
        self.checkBox_4.setFont(font)
        self.checkBox_4.setText("")
        self.checkBox_4.setObjectName("checkBox_4")
        MainWindow.setCentralWidget(self.centralwidget)
        self.menubar = QtWidgets.QMenuBar(MainWindow)
        self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
        self.menubar.setObjectName("menubar")
        MainWindow.setMenuBar(self.menubar)
        self.statusbar = QtWidgets.QStatusBar(MainWindow)
        self.statusbar.setObjectName("statusbar")
        MainWindow.setStatusBar(self.statusbar)
        
        self.checkBox.stateChanged.connect(self.check)
        self.checkBox_2.stateChanged.connect(self.check)
        self.checkBox_3.stateChanged.connect(self.check)
        self.checkBox_4.stateChanged.connect(self.check)

        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        
    def check(self):
        if self.checkBox.isChecked():
            self.checkBox_2.setChecked(False)
            self.checkBox_3.setChecked(False)
            self.checkBox_4.setChecked(False)
            
        elif self.checkBox_2.isChecked():
            self.checkBox.setChecked(False)
            self.checkBox_3.setChecked(False)
            self.checkBox_4.setChecked(False)
        
        elif self.checkBox_3.isChecked():
            self.checkBox.setChecked(False)
            self.checkBox_2.setChecked(False)
            self.checkBox_4.setChecked(False)
        
        elif self.checkBox_4.isChecked():
            self.checkBox.setChecked(False)
            self.checkBox_3.setChecked(False)
            self.checkBox_2.setChecked(False)
        
                
    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "Exam"))
        self.label.setText(_translate("MainWindow", "Option 01"))
        self.label_2.setText(_translate("MainWindow", "Option 02"))
        self.label_3.setText(_translate("MainWindow", "Option 03"))
        self.label_4.setText(_translate("MainWindow", "Option 04"))


if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    sys.exit(app.exec_())



Aucun commentaire:

Enregistrer un commentaire