'Setting radio buttons in pyqt5

I've been working on PyQt5 from the past few days - using Qt Designer and then converting file to .py and at last inserting functions to it , and it's fine for now .

But , I've now got some problems . I'm trying to create a multi-converting GUI using PyQt5 and given below is my code :

Part-1 ( Pre-written when converted to .py - Not posted due to body limit of words ) :

Part-2 ( Some hand-written code ) :

    def confirm(self):
        if self.radioButton.isChecked():
            list = ["gram","kilogram","pound","tonne"]

        elif self.radioButton_2.isChecked():
            list = ["centimetre","metre","kilometre","mile"]

        elif self.radioButton_3.isChecked():
            list = ["sq. metre","acre","hectare","sq. kilometre"]

        elif self.radioButton_4.isChecked():
            list = ["cubic mm (mm^3)","millilitre (ml)","cubic cm (litre)","cubic m (kilolitre)"]

        elif self.radioButton_5.isChecked():
            list = ["second","minute","hour","day"]

        elif self.radioButton_6.isChecked():
            list = ["celsius","farhenheit","kelvin","rankine"]

        elif self.radioButton_7.isChecked():
            list = ["INR","USD","GBP","PKR"]

        elif self.radioButton_8.isChecked():
            list = ["m/sec","km/hr","mach","mile/hr"]

        self.radioButton_9.setText(list[0])
        self.radioButton_10.setText(list[1])
        self.radioButton_11.setText(list[2])
        self.radioButton_12.setText(list[3])

        self.radioButton_13.setText(list[0])
        self.radioButton_14.setText(list[1])
        self.radioButton_15.setText(list[2])
        self.radioButton_16.setText(list[3])

    def OK(self):
        value = float(self.textEdit.toPlainText())

        if self.radioButton.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # gram to kilogram
                    answer = value/1000 ; unit = "kilogram"
                elif self.radioButton_15.isChecked(): # gram to pound
                    answer = value*2.2/1000 ; unit = "pound"
                elif self.radioButton_16.isChecked(): # gram to ton
                    answer = value/100000 ; unit = "ton"

                from_value = "gram"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # kilogram to gram
                    answer = value*1000 ; unit = "gram"
                elif self.radioButton_15.isChecked(): # kilogram to pound
                    answer = value*2.2 ; unit = "pound"
                elif self.radioButton_16.isChecked(): # kilogram to ton
                    answer = value/1000 ; unit = "ton"

                from_value = "kilogram"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # pound to gram
                    answer = value*450 ; unit = "gram"
                elif self.radioButton_14.isChecked(): # pound to kilogram
                    answer = value*0.45 ; unit = "kilogram"
                elif self.radioButton_16.isChecked(): # pound to ton
                    answer = value*0.0045 ; unit = "ton"

                from_value = "pound"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # ton to gram
                    answer = value*100000 ; unit = "gram"
                elif self.radioButton_14.isChecked(): # ton to kilogram
                    answer = value*100 ; unit = "kilogram"
                elif self.radioButton_15.isChecked(): # ton to pound
                    answer = value*2205 ; unit = "pound"

                from_value = "tonne"

        elif self.radioButton_2.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # centimetre to metre
                    answer = value/100 ; unit = "metre"
                elif self.radioButton_15.isChecked(): # centimetre to kilometre
                    answer = value/100000 ; unit = "kilometre"
                elif self.radioButton_16.isChecked(): # centimetre to mile
                    answer = value/160000 ; unit = "mile"

                from_value = "centimetre"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # metre to centimetre
                    answer = value*100 ; unit = "centimetre"
                elif self.radioButton_15.isChecked(): # metre to kilometre
                    answer = value/1000 ; unit = "kilometre"
                elif self.radioButton_16.isChecked(): # metre to mile
                    answer = value/1600 ; unit = "mile"

                from_value = "metre"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # kilometre to centimetre
                    answer = value*100000 ; unit = "centimetre"
                elif self.radioButton_14.isChecked(): # kilometre to metre
                    answer = value*1000 ; unit = "metre"
                elif self.radioButton_16.isChecked(): # kilometre to mile
                    answer = value*0.625 ; unit = "mile"

                from_value = "kilometre"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # mile to centimetre
                    answer = value*160000 ; unit = "centimetre"
                elif self.radioButton_14.isChecked(): # mile to metre
                    answer = value*1600 ; unit = "metre"
                elif self.radioButton_15.isChecked(): # mile to kilometre
                    answer = value*1.6 ; unit = "kilometre"

                from_value = "mile"

        elif self.radioButton_3.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # sq. metre to acre
                    answer = value/4000 ; unit = "acre"
                elif self.radioButton_15.isChecked(): # sq. metre to hectare 
                    answer = value/10000 ; unit = "hectare"
                elif self.radioButton_16.isChecked(): # sq. metre to sq. kilometre
                    answer = value/1000000 ; unit = "sq. km"

                from_value = "sq. m"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # acre to sq. metre
                    answer = value*4000 ; unit = "sq. m"
                elif self.radioButton_15.isChecked(): # acre to hectare
                    answer = value*0.4 ; unit = "hectare"
                elif self.radioButton_16.isChecked(): # acre to sq. kilometre
                    answer = value/250 ; unit = "sq. km"

                from_value = "acre"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # hectare to sq. metre
                    answer = value*10000 ; unit = "sq. m"
                elif self.radioButton_14.isChecked(): # hectare to acre
                    answer = value*2.5 ; unit = "acre"
                elif self.radioButton_16.isChecked(): # hectare to sq. kilometre
                    answer = value/100 ; unit = "sq. km"

                from_value = "hectare"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # sq. kilometre to sq. metre
                    answer = value*1000000 ; unit = "sq. m"
                elif self.radioButton_14.isChecked(): # sq. kilometre to acre
                    answer = value*250 ; unit = "acre"
                elif self.radioButton_15.isChecked(): # sq. kilometre to hectare
                    answer = value*100 ; unit = "hectare"

                from_value = "sq. km"

        elif self.radioButton_4.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # cubic mm to milli-litre
                    answer = value ; unit = "ml"
                elif self.radioButton_15.isChecked(): # cubic mm to litre
                    answer = value ; unit = "l"
                elif self.radioButton_16.isChecked(): # cubic mm to kilo-litre
                    answer = value ; unit = "kl"

                from_value = "cubic mm"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # milli-litre to cubic mm
                    answer = value ; unit = "cubic mm"
                elif self.radioButton_15.isChecked(): # milli-litre to litre
                    answer = value ; unit = "l"
                elif self.radioButton_16.isChecked(): # milli-litre to kilo-litre
                    answer = value ; unit = "kl"

                from_value = "millilitre"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # litre to cubic mm
                    answer = value ; unit = "cubic mm"
                elif self.radioButton_14.isChecked(): # litre to milli-litre
                    answer = value ; unit = "ml"
                elif self.radioButton_16.isChecked(): # litre to kilo-litre
                    answer = value ; unit = "kl"

                from_value = "litre"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # kilo-litre to cubic mm
                    answer = value ; unit = "cubic mm"
                elif self.radioButton_14.isChecked(): # kilo-litre to milli-litre
                    answer = value ; unit = "ml"
                elif self.radioButton_15.isChecked(): # kilo-litre to litre
                    answer = value ; unit = "l"

                from_value = "kilolitre"

        elif self.radioButton_5.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # second to minute
                    answer = value/60 ; unit = "minute"
                elif self.radioButton_15.isChecked(): # second to hour
                    answer = value/3600 ; unit = "hour"
                elif self.radioButton_16.isChecked(): # second to day
                    answer = value/86400 ; unit = "day"

                from_value = "second"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # minute to second
                    answer = value*60 ; unit = "second"
                elif self.radioButton_15.isChecked(): # minute to hour
                    answer = value/60 ; unit = "hour"
                elif self.radioButton_16.isChecked(): # minute to day
                    answer = value/1440 ; unit = "day"

                from_value = "minute"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # hour to second
                    answer = value*3600 ; unit = "second"
                elif self.radioButton_14.isChecked(): # hour to minute
                    answer = value*60 ; unit = "minute"
                elif self.radioButton_16.isChecked(): # hour to day
                    answer = value/24 ; unit = "day"

                from_value = "hour"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # day to second
                    answer = value*86400 ; unit = "second"
                elif self.radioButton_14.isChecked(): # day to minute
                    answer = value*1440 ; unit = "minute"
                elif self.radioButton_15.isChecked(): # day to hour
                    answer = value*24 ; unit = "hour"

                from_value = "day"

        elif self.radioButton_6.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # celsius to farhenheit
                    answer = value ; unit = "F"
                elif self.radioButton_15.isChecked(): # celsius to kelvin
                    answer = value+273.15 ; unit = "K"
                elif self.radioButton_16.isChecked(): # celsius to rankine
                    answer = value ; unit = "R"

                from_value = "C"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # farhenheit to celsius
                    answer = value ; unit = "C"
                elif self.radioButton_15.isChecked(): # farhenheit to kelvin
                    answer = value ; unit = "K"
                elif self.radioButton_16.isChecked(): # farhenheit to rankine
                    answer = value ; unit = "R"

                from_value = "F"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # kelvin to celsius
                    answer = value-273.15 ; unit = "C"
                elif self.radioButton_14.isChecked(): # kelvin to farhenheit
                    answer = value ; unit = "F"
                elif self.radioButton_16.isChecked(): # kelvin to rankine
                    answer = value ; unit = "R"

                from_value = "K"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # rankine to celsius
                    answer = value ; unit = "C"
                elif self.radioButton_14.isChecked(): # rankine to farhenheit
                    answer = value ; unit = "F"
                elif self.radioButton_15.isChecked(): # rankine to kelvin
                    answer = value ; unit = "K"

                from_value = "R"

        elif self.radioButton_7.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # INR to USD
                    answer = value/75 ; unit = "USD"
                elif self.radioButton_15.isChecked(): # INR to GBP
                    answer = value/100 ; unit = "GBP"
                elif self.radioButton_16.isChecked(): # INR to PKR
                    answer = value*2.5 ; unit = "PKR"

                from_value = "INR"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # USD to INR
                    answer = value*75 ; unit = "INR"
                elif self.radioButton_15.isChecked(): # USD to GBP
                    answer = value*0.75 ; unit = "GBP"
                elif self.radioButton_16.isChecked(): # USD to PKR
                    answer = value*187.5 ; unit = "PKR"

                from_value = "USD"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # GBP to INR
                    answer = value*100 ; unit = "INR"
                elif self.radioButton_14.isChecked(): # GBP to USD
                    answer = value*1.33 ; unit = "USD"
                elif self.radioButton_16.isChecked(): # GBP to PKR
                    answer = value*250 ; unit = "PKR"

                from_value = "GBP"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # PKR to INR
                    answer = value*0.4 ; unit = "INR"
                elif self.radioButton_14.isChecked(): # PKR to USD
                    answer = value/187.5 ; unit = "USD"
                elif self.radioButton_15.isChecked(): # PKR to GBP
                    answer = value/250 ; unit = "GBP"

                from_value = "PKR"

        elif self.radioButton_8.isChecked():
            if self.radioButton_9.isChecked():
                if self.radioButton_14.isChecked(): # m/sec to km/hr
                    answer = value*3.6 ; unit = "km/hr"
                elif self.radioButton_15.isChecked(): # m/sec to mach
                    answer = value*0.003 ; unit = "mach"
                elif self.radioButton_16.isChecked(): # m/sec to mile/hr
                    answer = value*2.24 ; unit = "mph"

                from_value = "m/sec"
                
            if self.radioButton_10.isChecked():
                if self.radioButton_13.isChecked(): # km/hr to m/sec
                    answer = value*0.27 ; unit = "m/sec"
                elif self.radioButton_15.isChecked(): # km/hr to mach
                    answer = value*0.0008 ; unit = "mach"
                elif self.radioButton_16.isChecked(): # km/hr to mile/hr
                    answer = value*0.625 ; unit = "mph"

                from_value = "km/hr"

            if self.radioButton_11.isChecked():
                if self.radioButton_13.isChecked(): # mach to m/sec
                    answer = value*340 ; unit = "m/sec"
                elif self.radioButton_14.isChecked(): # mach to km/hr
                    answer = value*1225 ; unit = "km/hr"
                elif self.radioButton_16.isChecked(): # mach to mile/hr
                    answer = value*761 ; unit = "mph"

                from_value = "mach"

            if self.radioButton_12.isChecked():
                if self.radioButton_13.isChecked(): # mile/hr to m/sec
                    answer = value*0.44 ; unit = "m/sec"
                elif self.radioButton_14.isChecked(): # mile/hr to km/hr
                    answer = value*1.6 ; unit = "km/hr"
                elif self.radioButton_15.isChecked(): # mile/hr to mach
                    answer = value*0.001 ; unit = "mach"

                from_value = "mile/hr"

            
        self.label_9.setText(f"{value} {from_value} = {answer} {unit} . ")

        with open("Calculator_History.txt","a+") as file:
            file.write(f"{value} {from_value} = {answer} {unit} .\n")
    
    def clearup(self):
        self.textEdit.setText("0")

        self.radioButton.setChecked(False)
        self.radioButton_2.setChecked(False)
        self.radioButton_3.setChecked(False)
        self.radioButton_4.setChecked(False)
        self.radioButton_5.setChecked(False)
        self.radioButton_6.setChecked(False)
        self.radioButton_7.setChecked(False)
        self.radioButton_8.setChecked(False)
        
        self.radioButton_9.setChecked(False)
        self.radioButton_10.setChecked(False)
        self.radioButton_11.setChecked(False)
        self.radioButton_12.setChecked(False)
        self.radioButton_13.setChecked(False)
        self.radioButton_14.setChecked(False)
        self.radioButton_15.setChecked(False)
        self.radioButton_16.setChecked(False)

Part-3 ( The ending - again pre-written ) :


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_())

Now coming to the point , the question is :

"How can I create a function which , on button click , will disable all radio buttons ( since I need them to restart the whole thing again without exiting ; and the function I wrote works only partially ) ?"

If anyone could help me regarding these , I would be deeply grateful to them ( like I was to a German guy who helped me with my periodic table ) .

Note : There is no such error in my code regarding the first part .



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source