Python Forum

Full Version: Dropdown menu- Store variable
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I have a program I'm trying to create that will ask for an IP address, run a check to ensure it's an IP address, then afterwards it pops up a box and asks a user to select whether they want to perform a basic or advanced search. Based on that selection, it will run different if statements.
I'm having a hard time trying to figure out how to store the selection in a variable. I tried an array, but as you can guess, that variable will equal both options, not what was selected.
I'm new to Python so any help with an explanation would be nice so I can learn.
Thanks!

class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title  = 'IP / Domain'
        self.left   = 50
        self.top    = 50
        self.width  = 640
        self.height = 480

        self.initUI()

    def initUI(self):
        self.setWindowTitle(self.title)
        self.setGeometry(self.left, self.top, self.width, self.height)

        self.label = QLabel()
        self.label.setStyleSheet("color: green; font: 16px;")

        layout = QVBoxLayout()
        layout.addWidget(self.label)
        layout.addWidget(QPushButton("Enter IP-address", clicked=self.getText))
        self.setLayout(layout)
        self.show()

    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):

        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')

        else:
            additionalText = "This is NOT an IP-address"
          

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

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')