Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/apache-kafka/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python PyQT5-将用户输入从窗口提示符存储到全局变量中_Python_Variables_Input_Pyqt5 - Fatal编程技术网

Python PyQT5-将用户输入从窗口提示符存储到全局变量中

Python PyQT5-将用户输入从窗口提示符存储到全局变量中,python,variables,input,pyqt5,Python,Variables,Input,Pyqt5,我有一个程序,将用户输入存储到一个变量中,检查它是否是一个IP地址,并从那里执行功能 我想添加一个窗口,使它看起来更漂亮,我很难从窗口存储变量 我希望能够- -收集用户输入并将其存储在变量中 -在函数中使用该变量检查它是否与IP地址匹配,如果匹配,则执行if语句 下面是我一直在玩的教程中的示例代码- import sys from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit from PyQt

我有一个程序,将用户输入存储到一个变量中,检查它是否是一个IP地址,并从那里执行功能

我想添加一个窗口,使它看起来更漂亮,我很难从窗口存储变量

我希望能够- -收集用户输入并将其存储在变量中 -在函数中使用该变量检查它是否与IP地址匹配,如果匹配,则执行if语句

下面是我一直在玩的教程中的示例代码-

    import sys
from PyQt5.QtWidgets import QApplication, QWidget, QInputDialog, QLineEdit
from PyQt5.QtGui import QIcon


class App(QWidget):

    def __init__(self):
        super().__init__()
        self.title = 'IP / Domain'
        self.left = 10
        self.top = 10
        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.getInteger()
        self.getText()
        self.getDouble()
        self.getChoice()

        self.show()   

    def getText(self):  

        userInput, okPressed = QInputDialog.getText(self, "Get text", "Your name:", QLineEdit.Normal, "")
        if okPressed and text != '':
            print(userInput)

def ipFormatChk(userInput): #input
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)
#do something
return

if __name__ == '__main__':
    app = QApplication(sys.argv)
    ex = App()
    sys.exit(app.exec_())
试试看:

import sys
import re
from PyQt5.QtWidgets import (QApplication, QWidget, QInputDialog, QLineEdit, 
                             QLabel, QVBoxLayout, QPushButton)
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.getInteger()
        #self.getText()
        #self.getDouble()
        #self.getChoice()

        self.show()   

    def getText(self):  

        userInput, okPressed = QInputDialog.getText(
                self, 
                "Input IP-address", 
                "Your IP-address:", 
                QLineEdit.Normal, "")
        if okPressed:                       # and userInput != '':
            #print(userInput)
            if userInput.strip():
                self.ipFormatChk(userInput)
            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):
            additionalText = "This is IP-address"
            self.label.setStyleSheet("color: lightgreen; font: 24px;")
        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_())
导入系统 进口稀土 从PyQt5.QtWidgets导入(QApplication、QWidget、QInputDialog、QLineEdit、, QLabel、QVBoxLayout、QPushButton) 从PyQt5.QtGui导入QIcon 类应用程序(QWidget): 定义初始化(自): super()。\uuuu init\uuuuu() self.title='IP/域' self.left=50 self.top=50 自宽=640 自身高度=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(“颜色:绿色;字体:16px;”) layout=QVBoxLayout() layout.addWidget(self.label) layout.addWidget(QPushButton(“输入IP地址”,clicked=self.getText)) self.setLayout(布局) #self.getInteger() #self.getText() #self.getDouble() #self.getChoice() self.show() def getText(self): userInput,okPressed=QInputDialog.getText( 自己 “输入IP地址”, “您的IP地址:”, QLineEdit.Normal(“”) 如果按OK:#和userInput!='': #打印(用户输入) 如果userInput.strip(): self.ipFormatChk(用户输入) 其他: self.label.setStyleSheet(“颜色:红色;字体:24px;”) self.label.setText(“输入行为空,输入IP地址”) 其他: self.label.setText(“”) def ipFormatChk(自我、用户输入): 模式=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” 如果重新匹配(模式、用户输入): additionalText=“这是IP地址” self.label.setStyleSheet(“颜色:浅绿色;字体:24px;”) 其他: additionalText=“这不是IP地址” self.label.setStyleSheet(“颜色:红色;字体:24px;”)
self.label.setText(“{}太棒了,谢谢!这很有效!我现在有一些研究要做。祝你好运作为一个后续问题,我如何添加一个包含高级和基本选项的下拉框,并将其分配给一个变量?我正试图在GUI方面绞尽脑汁,这让我发疯。