我添加了一个下拉框,因此在单击按钮后,它将提示您输入IP地址,并在下拉框中提供一些选项。
添加下拉框后,应用程序崩溃。我正在尝试将他们选择的选项存储到变量中。
选择该选项并检查IP地址后,将从该位置执行一些操作。
import sys
import re
from PyQt5.QtCore import QRect
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog, QLineEdit,
QLabel, QVBoxLayout, QPushButton, QComboBox)
from PyQt5.QtGui import QIcon
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, "")
centralWidget = QWidget(self)
userselection,self.setCentralWidget(centralWidget)
userselection,self.comboBox = QComboBox(centralWidget)
userselection,self.comboBox.setGeometry(QRect(40, 40, 491, 31))
userselection,self.comboBox.setObjectName(("ip"))
userselection,self.comboBox.addItem("domain")
if okPressed: # and userInput != '':
#print(userInput)
if userInput.strip():
self.ipFormatChk(userInput)
在此处为IP地址运行代码
else:
self.label.setStyleSheet("color: red; font: 24px;")
self.label.setText("Input line is empty, enter IP-address")
else:
self.label.setText("")
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) and userselection is 'ip':
additionalText = "This is IP-address"
self.label.setStyleSheet("color: lightgreen; font: 24px;")
if re.match(pattern, userInput) and userselection is 'ip':
在此处执行操作。
else:
additionalText = "This is NOT an IP-address"
self.label.setStyleSheet("color: red; font: 24px;")
self.label.setText("{} <- {}".format(userInput, additionalText))
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = App()
sys.exit(app.exec_())