Python Forum
Dropdown menu- Store variable
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))
Reply
#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')
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Dropdown box showing weird entry cybertooth 4 2,177 Aug-16-2021, 03:45 PM
Last Post: deanhystad
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,541 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] populating dropdown from Method mikisDW 2 3,813 Apr-06-2020, 08:06 PM
Last Post: mikisDW
  Option dropdown with Pyinquerer julio2000 0 1,512 Mar-22-2020, 04:11 PM
Last Post: julio2000
  [Tkinter] Choose from dropdown list and then do something? Selfiatus1 2 5,401 Jun-07-2019, 08:43 PM
Last Post: Selfiatus1
  printing option menu variable in label in Tkinter SmokerX 1 6,591 Jan-18-2018, 07:36 PM
Last Post: SmokerX

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020