Python PyQT5-添加下拉框,现在应用程序崩溃

Python PyQT5-添加下拉框,现在应用程序崩溃,python,window,pyqt5,Python,Window,Pyqt5,我添加了一个下拉框,因此单击按钮后,它将提示输入IP地址,并在下拉框中为您提供一些选项 添加下拉框后,应用程序崩溃。我试图将他们选择的选项存储到一个变量中 一旦选择该选项并检查IP地址,它将从那里执行一些操作 import sys import re from PyQt5.QtCore import QRect from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog, QLineEdit,

我添加了一个下拉框,因此单击按钮后,它将提示输入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_())
其他:
additionalText=“这不是IP地址”
self.label.setStyleSheet(“颜色:红色;字体:24px;”)

self.label.setText(“{}为什么不在控制台中运行代码并读取回溯?找到了一个解决方法。将其更改为在输入IP地址后弹出下拉框。