Search code examples
pythonpyqt5qt-designer

pyqt5: execute a local function for evaluation by accessing the values entered in GUI widgets


I am developing a GUI with some drop downs and Line-edit fields. I can get it working. The scenario is that, with the entered values(all on button presses or keyboard typing), I want to evaluate these values and then activate the 'execute tool' button in GUI.

The problem is that I have no button or any interrupt for this evaluate function. It should be done automatically after all fields are filled.

I have a separate function for evaluation. But I am not knowing how to call this simple python 'evaluation' function in between GUI process.

Similar sample image

    class ApplicationWindow:
        def __init__(self):
          ### .... init variables
    
        def main_gui(self):
        # creating a qt gui widget application Object to display - Config setup
        app = QtWidgets.QApplication(sys.argv)
        ... # All basic stuff for GUI setup, init & connect on key press
        self.gui.i_year_num.activated.connect(lambda: self.i_year_fun())
        self.gui.i_month_num.activated.connect(lambda: self.i_month_fun())
        # self.evaluate()       # Calling here executes this fun before even GUI is opened
        sys.exit(app.exec_())
        
        def evaluate(self):
            if ((self.i_year is not None)
                    and (self.i_month is not None) and .... #conditions# ):
                print("All fields are not empty")
                self.i_flag = True
    
            if ((self.i_year == '')
                    or (self.i_month == '') or .... #conditions#):
                self.showInfoDialog('Error', ' All fields should be filled! ')
    
            self.file_name_tag = str(self.i_name) + '-' + str(self.i_year) + '-' \
                                 + str(self.i_month) + '-' + str(self.version_id)
            print("Name tag is ", self.file_name_tag)


# The gui is called here
ApplicationWindow_object = ApplicationWindow()
ApplicationWindow_object.main_gui()

Solution

  • You must create a method that validates all the inputs and invoke it every time something changes in the inputs you want to monitor:

        self.gui.button.setEnabled(False)
        # connections
        self.gui.i_year_num.activated.connect(self.verify_validate)
        self.gui.i_month_num.activated.connect(self.verify_validate)
    
    def verify_validate(self):
        is_valid = self.validate()
        self.gui.button.setEnabled(is_valid)
        if not is_valid:
            self.showInfoDialog('Error', ' All fields should be filled! ')
    
    
    def validate(self):
        if not all(self.gui.i_year_num, self.gui.i_month):
            # some input is empty
            return False
        # other checks
        return True