PyQT5 - Добавлен выпадающий список, и теперь приложение вылетает - PullRequest
0 голосов
/ 06 сентября 2018

Я добавил раскрывающийся список, чтобы после нажатия кнопки он запрашивал 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_())
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...