Python/Pyqt5- 将选定的下拉选项存储在变量中
Python/Pyqt5- Store selected dropdown option in variable
我正在尝试 运行 根据用户 selects "Basic" 或 "Advanced".
的不同 if 语句
到目前为止,这是我的代码。我将使用 advanced/basic 选项,因此我可以执行类似以下的操作并将其存储在一个函数中,然后在需要时稍后调用它。
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')
这是我原来的工作代码。发生的事情是,因为它是一个数组,它总是等于 [0] 和 [1],我试图弄清楚如何存储它们 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_())
将结果与数组中的项目进行比较。
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')
注意:我可能建议将两个 if
语句放在一个封闭的 if okPressed
块中,因为它可能会 select Cancel
或任何不合适的选项是。
如果以后需要保存,直接保存到自己的实例变量中即可:
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')
我希望这能回答你的问题
我正在尝试 运行 根据用户 selects "Basic" 或 "Advanced".
的不同 if 语句到目前为止,这是我的代码。我将使用 advanced/basic 选项,因此我可以执行类似以下的操作并将其存储在一个函数中,然后在需要时稍后调用它。
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')
这是我原来的工作代码。发生的事情是,因为它是一个数组,它总是等于 [0] 和 [1],我试图弄清楚如何存储它们 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_())
将结果与数组中的项目进行比较。
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')
注意:我可能建议将两个 if
语句放在一个封闭的 if okPressed
块中,因为它可能会 select Cancel
或任何不合适的选项是。
如果以后需要保存,直接保存到自己的实例变量中即可:
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')
我希望这能回答你的问题