1
\$\begingroup\$

Situation

I have a large GUI code consisting of a combobox. Whenever I select an option in the combobox, say X, in the very same main window, just next to combobox, it displays a corresponding groupbox, say GB_X. That groupbox consists of a lot of further options asking input from the user.

What have I done until Now?

I broke that large code into two small codes:Code 1 and Code 2.

Code 1: It has the main window with the combobox with different option, say X, Y, Z

Code 2: Consists of the groupbox, say GB_X, which further has many widgets.

Main Problem!

I wish to restructure the large code into two smaller codes which I have done. But now I wish to call Code 2 from Code 1 that is when I run Code 1, Code 2 should also be executed.

Whenever I should choose X in the combobox in Code 1, I want it to call Code 2(in some other file) which displays the GB_X and consists of the widgets enclosed in it.

I know if I do it method by method, I can do it individually but is there any easier way?

Sample code:

Code 1

from PyQt5 import QtCore, QtGui, QtWidgets
        
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(708, 462)
        MainWindow.setFocusPolicy(QtCore.Qt.WheelFocus)
        MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.VP_push_close = QtWidgets.QPushButton(self.centralwidget)
        self.VP_push_close.setObjectName("VP_push_close")
        self.gridLayout_2.addWidget(self.VP_push_close, 3, 1, 1, 1)
        self.line_separate = QtWidgets.QFrame(self.centralwidget)
        self.line_separate.setFrameShape(QtWidgets.QFrame.HLine)
        self.line_separate.setFrameShadow(QtWidgets.QFrame.Sunken)
        self.line_separate.setObjectName("line_separate")
        self.gridLayout_2.addWidget(self.line_separate, 1, 0, 1, 2)
        self.gridLayout = QtWidgets.QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label_DefType = QtWidgets.QLabel(self.centralwidget)
        self.label_DefType.setStyleSheet("font: 75 8pt \"MS Shell Dlg 2\";")
        self.label_DefType.setObjectName("label_DefType")
        self.gridLayout.addWidget(self.label_DefType, 0, 0, 1, 1)
        self.combo_Deftype = QtWidgets.QComboBox(self.centralwidget)
        self.combo_Deftype.setObjectName("combo_Deftype")
        self.combo_Deftype.addItem("")
        self.combo_Deftype.addItem("")
        self.combo_Deftype.addItem("")
        self.gridLayout.addWidget(self.combo_Deftype, 0, 1, 1, 1)
        spacerItem = QtWidgets.QSpacerItem(148, 20, QtWidgets.QSizePolicy.Expanding, QtWidgets.QSizePolicy.Minimum)
        self.gridLayout.addItem(spacerItem, 0, 2, 1, 1)
        self.label_MDOCLink = QtWidgets.QLabel(self.centralwidget)
        self.label_MDOCLink.setStyleSheet("font: 75 8pt \"MS Shell Dlg 2\";")
        self.label_MDOCLink.setObjectName("label_MDOCLink")
        self.gridLayout.addWidget(self.label_MDOCLink, 0, 3, 1, 1)
        self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1)
        MainWindow.setCentralWidget(self.centralwidget)
        self.label_DefType.setBuddy(self.combo_Deftype)
        self.retranslateUi(MainWindow)
        self.VP_push_close.clicked.connect(MainWindow.close)                              #Close Button
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        #######################################################   
        # Set the HYPERLINK
        path_Ref = r"C:/Users/ABCD/Documents/ABCD.pdf"
        url_Ref  = bytearray(QtCore.QUrl.fromLocalFile(path_Ref).toEncoded()).decode()
        text_Ref = "<a href={}> Reference Link </a>".format(url_Ref)
        self.label_MDOCLink.setText(text_Ref)
        self.label_MDOCLink.setOpenExternalLinks(True)

        #Combo Box Specificity
        self.GB_X.hide()        
        self.combo_Deftype.activated[str].connect(self.specificity)      
      #######################################################

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        
        self.VP_push_close.setText(_translate("MainWindow", "Close"))
        self.label_DefType.setText(_translate("MainWindow", "Choose the Type"))
        self.combo_Deftype.setItemText(0, _translate("MainWindow", "-"))
        self.combo_Deftype.setItemText(1, _translate("MainWindow", "X"))
        self.combo_Deftype.setItemText(2, _translate("MainWindow", "Y"))

        #######################################################      
    def specificity(self, text):
        if  text == 'X':
            self.GB_X.show()  
        #######################################################

if __name__ == "__main__":
    import sys

        #######################################################    
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()
        #######################################################

Code 2

from PyQt5 import QtCore, QtGui, QtWidgets
        
class Ui_MainWindow(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName("MainWindow")
        MainWindow.resize(708, 462)
        MainWindow.setFocusPolicy(QtCore.Qt.WheelFocus)
        MainWindow.setTabShape(QtWidgets.QTabWidget.Rounded)
        self.centralwidget = QtWidgets.QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.gridLayout_2 = QtWidgets.QGridLayout(self.centralwidget)
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.GB_X = QtWidgets.QGroupBox(self.centralwidget)
        self.GB_X.setMaximumSize(QtCore.QSize(741, 16777215))
        self.GB_X.setFocusPolicy(QtCore.Qt.StrongFocus)
        self.GB_X.setObjectName("GB_X")
        self.gridLayout_4 = QtWidgets.QGridLayout(self.GB_X)
        self.gridLayout_4.setObjectName("gridLayout_4")
        self.X_gridLayout_Abb = QtWidgets.QGridLayout()
        self.X_gridLayout_Abb.setObjectName("X_gridLayout_Abb")
        self.X_table_Display = QtWidgets.QTableWidget(self.GB_X)
        self.X_table_Display.setColumnCount(4)
        self.X_table_Display.setObjectName("X_table_Display")
        self.X_table_Display.setRowCount(0)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        item.setFont(font)
        self.X_table_Display.setHorizontalHeaderItem(0, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        item.setFont(font)
        self.X_table_Display.setHorizontalHeaderItem(1, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        item.setFont(font)
        self.X_table_Display.setHorizontalHeaderItem(2, item)
        item = QtWidgets.QTableWidgetItem()
        item.setTextAlignment(QtCore.Qt.AlignCenter)
        font = QtGui.QFont()
        font.setBold(True)
        font.setWeight(75)
        item.setFont(font)
        self.X_table_Display.setHorizontalHeaderItem(3, item)
        self.gridLayout_4.addWidget(self.X_table_Display, 2, 0, 1, 1)
        self.X_gridLayout_DefProp = QtWidgets.QGridLayout()
        self.X_gridLayout_DefProp.setObjectName("X_gridLayout_DefProp")
        self.X_CB_Modulus = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_Modulus.setObjectName("X_CB_Modulus")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_Modulus, 2, 3, 1, 1)
        self.X_CB_DT = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_DT.setObjectName("X_CB_DT")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_DT, 2, 1, 1, 2)
        self.X_CB_FHC = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_FHC.setObjectName("X_CB_FHC")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_FHC, 2, 5, 1, 1)
        self.X_label_Criteria = QtWidgets.QLabel(self.GB_X)
        self.X_label_Criteria.setStyleSheet("font: 75 8pt \"MS Shell Dlg 2\";")
        self.X_label_Criteria.setObjectName("X_label_Criteria")
        self.X_gridLayout_DefProp.addWidget(self.X_label_Criteria, 2, 0, 1, 1)
        self.X_line_Att = QtWidgets.QLineEdit(self.GB_X)
        self.X_line_Att.setObjectName("X_line_Att")
        self.X_gridLayout_DefProp.addWidget(self.X_line_Att, 1, 1, 1, 3)
        self.X_CB_PT = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_PT.setObjectName("X_CB_PT")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_PT, 2, 4, 1, 1)
        self.X_CB_PC = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_PC.setObjectName("X_CB_PC")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_PC, 2, 6, 1, 1)
        self.X_CB_OHT = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_OHT.setObjectName("X_CB_OHT")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_OHT, 2, 7, 1, 1)
        self.X_CB_FHT = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_FHT.setObjectName("X_CB_FHT")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_FHT, 2, 8, 1, 1)
        self.X_CB_Bearing = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_Bearing.setObjectName("X_CB_Bearing")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_Bearing, 2, 9, 1, 1)
        self.X_CB_Pull = QtWidgets.QCheckBox(self.GB_X)
        self.X_CB_Pull.setObjectName("X_CB_Pull")
        self.X_gridLayout_DefProp.addWidget(self.X_CB_Pull, 2, 10, 1, 1)
        self.X_push_clear = QtWidgets.QPushButton(self.GB_X)
        self.X_push_clear.setObjectName("X_push_clear")
        self.X_gridLayout_DefProp.addWidget(self.X_push_clear, 3, 5, 1, 2)
        self.X_label_Att = QtWidgets.QLabel(self.GB_X)
        self.X_label_Att.setStyleSheet("font: 75 8pt \"MS Shell Dlg 2\";")
        self.X_label_Att.setObjectName("X_label_Att")
        self.X_gridLayout_DefProp.addWidget(self.X_label_Att, 1, 0, 1, 1)
        self.X_label_Thickness = QtWidgets.QLabel(self.GB_X)
        self.X_label_Thickness.setStyleSheet("font: 75 8pt \"MS Shell Dlg 2\";")
        self.X_label_Thickness.setObjectName("X_label_Thickness")
        self.X_gridLayout_DefProp.addWidget(self.X_label_Thickness, 0, 0, 1, 1)
        self.X_radio_grt5 = QtWidgets.QRadioButton(self.GB_X)
        self.X_radio_grt5.setObjectName("X_radio_grt5")
        self.X_gridLayout_DefProp.addWidget(self.X_radio_grt5, 0, 1, 1, 1)
        self.X_radio_less5 = QtWidgets.QRadioButton(self.GB_X)
        self.X_radio_less5.setObjectName("X_radio_less5")
        self.X_gridLayout_DefProp.addWidget(self.X_radio_less5, 0, 2, 1, 1)
        self.gridLayout_4.addLayout(self.X_gridLayout_DefProp, 1, 0, 1, 1)
        self.gridLayout_2.addWidget(self.GB_X, 2, 0, 1, 2)
        
        MainWindow.setCentralWidget(self.centralwidget)
        self.X_label_Att.setBuddy(self.X_line_Att)
        self.retranslateUi(MainWindow)
        QtCore.QMetaObject.connectSlotsByName(MainWindow)
        #######################################################   
        #Radio Button and Line Edit connection
        #self.X_radio_grt5.setChecked(True)
        self.X_line_Att.setDisabled(True)
        self.X_radio_grt5.toggled.connect(lambda:self.X_radio_grt5_clicked(self.X_radio_grt5))    
        self.X_radio_less5.toggled.connect(lambda:self.X_radio_less5_clicked(self.X_radio_less5))
        
        #Check Boxs and forming the listof chosen ceiterion
        Chkbox = [self.X_CB_Modulus,self.X_CB_PC,self.X_CB_FHC,self.X_CB_DT,self.X_CB_PT,self.X_CB_OHT,self.X_CB_FHT,self.X_CB_Bearing,self.X_CB_Pull]   
                
        for name in Chkbox:
            name.setDisabled(True)
        self.X_CB_Modulus.clicked.connect(lambda:self.addRow(self.X_CB_Modulus))
        self.X_CB_PC.clicked.connect(lambda:self.addRow(self.X_CB_PC))
        self.X_CB_FHC.clicked.connect(lambda:self.addRow(self.X_CB_FHC))
        self.X_CB_DT.clicked.connect(lambda:self.addRow(self.X_CB_DT))
        self.X_CB_PT.clicked.connect(lambda:self.addRow(self.X_CB_PT))
        self.X_CB_OHT.clicked.connect(lambda:self.addRow(self.X_CB_OHT))
        self.X_CB_FHT.clicked.connect(lambda:self.addRow(self.X_CB_FHT))
        self.X_CB_Bearing.clicked.connect(lambda:self.addRow(self.X_CB_Bearing))
        self.X_CB_Pull.clicked.connect(lambda:self.addRow(self.X_CB_Pull ))

        #Disable the table widget for further editing
        self.X_table_Display.setEditTriggers(QtWidgets.QTableWidget.NoEditTriggers)

        #Clear Button
        #self.X_push_clear.clicked.connect(self.X_table_Display.clearContents)            
        self.X_push_clear.clicked.connect(lambda:self.clear_table(self.X_push_clear))
        
      #######################################################

    def retranslateUi(self, MainWindow):
        _translate = QtCore.QCoreApplication.translate
        MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
        self.GB_X.setTitle(_translate("MainWindow", "GB_X"))
        item = self.X_table_Display.horizontalHeaderItem(0)
        item.setText(_translate("MainWindow", "Thickness"))
        item = self.X_table_Display.horizontalHeaderItem(1)
        item.setText(_translate("MainWindow", "Atennuation"))
        item = self.X_table_Display.horizontalHeaderItem(2)
        item.setText(_translate("MainWindow", "Criteria"))
        item = self.X_table_Display.horizontalHeaderItem(3)
        item.setText(_translate("MainWindow", "KDF"))
        self.X_CB_Modulus.setText(_translate("MainWindow", "Modulus"))
        self.X_CB_DT.setText(_translate("MainWindow", "DT:(CAI/CAIE)"))
        self.X_CB_FHC.setText(_translate("MainWindow", "FHC"))
        self.X_label_Criteria.setText(_translate("MainWindow", "Criteria"))
        self.X_CB_PT.setText(_translate("MainWindow", "PT"))
        self.X_CB_PC.setText(_translate("MainWindow", "PC"))
        self.X_CB_OHT.setText(_translate("MainWindow", "OHT"))
        self.X_CB_FHT.setText(_translate("MainWindow", "FHT"))
        self.X_CB_Bearing.setText(_translate("MainWindow", "Bearing"))
        self.X_CB_Pull.setText(_translate("MainWindow", "Pull Through"))
        self.X_push_clear.setText(_translate("MainWindow", "Clear"))
        self.X_label_Att.setText(_translate("MainWindow", "Att (in dB)"))
        self.X_label_Thickness.setText(_translate("MainWindow", "Thickness"))
        self.X_radio_grt5.setText(_translate("MainWindow", ">5mm"))
        self.X_radio_less5.setText(_translate("MainWindow", "<=5mm"))

        ####################################################### 
   
    def X_radio_grt5_clicked(self,enabled):
        if enabled:
            self.X_line_Att.setDisabled(False)
            Chkbox = [self.X_CB_Modulus,self.X_CB_PC,self.X_CB_FHC,self.X_CB_DT,self.X_CB_PT,self.X_CB_OHT,self.X_CB_FHT,self.X_CB_Bearing,self.X_CB_Pull]
            for name in Chkbox:
                name.setDisabled(False)
            self.X_line_Att.setValidator(QtGui.QDoubleValidator(-24,-12,2))
            
    def X_radio_less5_clicked(self,enabled):
        if enabled:
            self.X_line_Att.setDisabled(False)
            Chkbox = [self.X_CB_Modulus,self.X_CB_PC,self.X_CB_FHC,self.X_CB_DT,self.X_CB_PT,self.X_CB_OHT,self.X_CB_FHT,self.X_CB_Bearing,self.X_CB_Pull] 
            for name in Chkbox:
                name.setDisabled(False)
            self.X_line_Att.setValidator(QtGui.QDoubleValidator(-24,-6,2))
            
    def addRow(self,button):
        if button.isChecked():
            
            #Take thickness radio button into accound to get thickness value
            if self.X_radio_grt5.isChecked():
                a_thickness   =  self.X_radio_grt5.text()
            elif self.X_radio_less5.isChecked():
                a_thickness   =  self.X_radio_less5.text()
            
            #Att
            b_Att = self.X_line_Att.text()
            
            #Criteria
            c_criteria    = button.text()
            
            
            row_pos = self.X_table_Display.rowCount()
            self.X_table_Display.insertRow(row_pos)
            
            listi = cal_X.calc_X(str(a_thickness),int(b_Att),str(c_criteria))            
            
            #Display Items in Table
            self.X_table_Display.setItem(row_pos, 0,QtWidgets.QTableWidgetItem(listi[0]))
            self.X_table_Display.setItem(row_pos, 1,QtWidgets.QTableWidgetItem(listi[1]))
            self.X_table_Display.setItem(row_pos, 2,QtWidgets.QTableWidgetItem(listi[2]))
            self.X_table_Display.setItem(row_pos, 3,QtWidgets.QTableWidgetItem(listi[3]))         

        else:
            #Remove items if unchecked the button
            rows = self.X_table_Display.rowCount()
            if rows:                                #Ensure that variable rows has some value
                for i in range(0,rows,1):
                    txti = str(self.X_table_Display.item(i,2).text())
                    if txti == str(button.text()):
                        self.X_table_Display.removeRow(i)
                    elif txti == '':
                        self.X_table_Display.removeRow(i)    

    def clear_table(self,button):
        self.X_table_Display.setRowCount(0)
        
        Chkbox = [self.X_CB_Modulus,self.X_CB_PC,self.X_CB_FHC,self.X_CB_DT,self.X_CB_PT,self.X_CB_OHT,self.X_CB_FHT,self.X_CB_Bearing,self.X_CB_Pull]   
        
        for name in Chkbox:
            name.setChecked(False)                        
        #######################################################

if __name__ == "__main__":
    import sys

        #######################################################    
    app = QtCore.QCoreApplication.instance()
    if app is None:
        app = QtWidgets.QApplication(sys.argv)

#    app = QtWidgets.QApplication(sys.argv)
    MainWindow = QtWidgets.QMainWindow()
    ui = Ui_MainWindow()
    ui.setupUi(MainWindow)
    MainWindow.show()
    app.exec_()
        #######################################################

Sorry, I know it is a little length post, but an help will be appreciated.

PS: Anything between the long hash lines is a code added by me to code generated by using the Qt designer.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.