Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Dropdown menu- Store variable
#1
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))
Quote
#2
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')
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  [Tkinter] Choose from dropdown list and then do something? Selfiatus1 2 241 Jun-07-2019, 08:43 PM
Last Post: Selfiatus1
  printing option menu variable in label in Tkinter SmokerX 1 2,237 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:


Users browsing this thread: 1 Guest(s)