Python/Pyqt5- Store selected dropdown option in variable

1.2k views Asked by At

I am trying to run different if statements based off of whether the user selects "Basic" or "Advanced".

Here is my code so far. I'll be using the advanced/basic options so I may do something like the following and store it in a function and then just call it later on when it's needed.

def basicAdvOpt
    advBasicOptions = ("Basic", "Advanced") 

    selection, okPressed = QInputDialog.getItem(self, "Select Basic or Advanced", "", advBasicOptions, 0, False)
    if selection[0]:
        print('Basic')

    if selection[1]:
        print('advanced')

Here is my original working code. What happens is, because it's an array, it will always equal [0] and [1] and I'm trying to figure out how to store the value of whatever they select.

    def getText(self):
        userInput, okPressed = QInputDialog.getText( self,"Input IP-address", "Your IP-address:",QLineEdit.Normal, "")
        if okPressed:
            self.ipFormatChk(userInput)     #Pass the userInput variable 
                                            into the ipFormatChk function

            if userInput.strip():
                self.ipFormatChk(userInput)


    def ipFormatChk(self, userInput):  #Checks if the input matches IP 
                                         format

        pattern = r"\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\." \
                  r"(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\b"

        if re.match(pattern, userInput):
            additionalText = "This is IP-address"
            self.label.setStyleSheet("color: lightgreen; font: 24px;")

            advBasicOptions = ("Basic", "Advanced") 
            selection, okPressed = QInputDialog.getItem(self, "Select Basic or Advanced", "", advBasicOptions, 0, False)
            if selection[0]:
                print('Basic')

            if selection[1]:
                print('advanced')
#Or just call the function


            basicAdvOpt()

        else:
            additionalText = "This is NOT an IP-address"
            self.label.setStyleSheet("color: red; font: 24px;")
            advBasic = ("Basic", "Advanced")
            advBasic, okPressed = QInputDialog.getItem(self, "Select Basic or Advanced", "", advBasic, 0, False)

        self.label.setText("{} <- {}".format(userInput, additionalText))


if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex  = App()
    sys.exit(app.exec_())
1

There are 1 answers

0
Jordan Epstein On BEST ANSWER

compare the result to the items in the array.

advBasicOptions = ("Basic", "Advanced") 
selection, okPressed = QInputDialog.getItem(self, "Select Basic or Advanced", "", advBasicOptions, 0, False)

if selection == advBasicOptions[0]:
    print('Basic')
if selection == advBasicOptions[1]:
    print('advanced')

note: I might suggest putting both of the if statements in an enclosing if okPressed block because it is possible would select Cancel or whatever the non-Ok option is.

If you need to store it for later, just save it to an instance variable on self:

advBasicOptions = ("Basic", "Advanced") 
self.selection, okPressed = QInputDialog.getItem(self, "Select Basic or Advanced", "", advBasicOptions, 0, False)

if self.selection == advBasicOptions[0]:
    print('Basic')
if self.selection == advBasicOptions[1]:
    print('advanced')

I hope this answers your question