简体   繁体   English

检查输入Pyqt4

[英]Checking input Pyqt4

I want to check if the user has inputed a string if an integer is enetered then an error is thrown asking the user to re-enter a string so the okay button is rejected I also want the same if nothing is inputed by the user so no string. 我想检查用户是否输入了字符串(如果输入了整数),然后引发了一个错误,要求用户重新输入字符串,所以可以的按钮被拒绝。如果用户没有输入任何内容,我也希望输入相同的字符串,所以没有串。

import sys
from PyQt4 import QtGui


class Example(QtGui.QWidget):

    def __init__(self):
        super(Example, self).__init__()

        self.initUI()

    def initUI(self):      

        self.btn = QtGui.QPushButton('Dialog', self)
        self.btn.move(20, 20)
        self.btn.clicked.connect(self.showDialog)

        self.le = QtGui.QLineEdit(self)
        self.le.move(130, 22)

        self.setGeometry(300, 300, 290, 150)
        self.setWindowTitle('Input dialog')
        self.show()

    def showDialog(self):

        text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
            'Enter your name:')

        if ok:
            self.le.setText(str(text))

def main():

    app = QtGui.QApplication(sys.argv)
    ex = Example()
    sys.exit(app.exec_())


if __name__ == '__main__':
    main()

How do I apply this to the above code? 如何将其应用于上述代码?

Im using Pyqt4, for Python 3. 我使用的是Pyqt4,适用于Python 3。

Thanks in advance 提前致谢

I don't have PyQt4, but I'm assuming my code samples are still applicable. 我没有PyQt4,但是我假设我的代码示例仍然适用。 But either way, the code you have given accepts (If I'm not mistaken) the name as a string. 但是无论哪种方式,您提供的代码都会接受(如果我没有记错的话)名称作为字符串。 So, if you check if it's a integer or not, it will never be an integer, due to you accepting an answer as a string. 因此,如果您检查它是否为整数,由于您接受答案作为字符串,因此它永远不会是整数。

As for the being blank thing, here's my code: 至于空白,这是我的代码:

#Start a while loop to repeatedly ask.
while True:

    #A flag to help with detecting if it's a blank string.
    noneFlag = True

    #The equivalent of your  
    name = input("Your name: ")

    #A for loop - It checks if the string is nothing but blanks, or is nothing. 
    for char in name:

        if char != " ":
            noneFlag = False
            break

    #Responds on if the name is blank or not.
    if noneFlag:
        print("Name field is blank.")
    else:
        #Breaks the loop.
        break

NOTE: Bear in mind that your line: 注意:请记住您的行:

text, ok = QtGui.QInputDialog.getText(self, 'Input Dialog', 
        'Enter your name:')

is the equivalent of my input statement. 等价于我的输入语句。

--- EDIT --- -编辑-

Okay, here's the code you wanted; 好的,这是您想要的代码; although bear in mind that unless you change to code to accept the variable as an int, it will never view it as an integer. 尽管请记住,除非您更改为将变量接受为int的代码,否则它将永远不会将其视为整数。

if noneFlag:
    print("Name field is blank.")
elif isinstance(name, int):
    print("Name field is an int.")
else:
    break

In essence, what I'm saying is that Python cannot and will not detect if a variable is numbers if it's in a string. 本质上,我要说的是,Python无法且不会检测到如果变量在字符串中是否为数字。 For example: "1234567890" as a STRING will not be classed as an integer, as the actual type is a string. 例如:“ 1234567890”作为STRING不会被归类为整数,因为实际类型是字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM