PyQt5和Python中的用户输入验证 [英] User Input Validation in PyQt5 and Python

查看:262
本文介绍了PyQt5和Python中的用户输入验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是关于输入验证的一个分为两个部分的问题,其中包含一个特定的组件和另一个更通用的组件.

This is a two part question about input validation with one specific and another more general component.

具体信息:

在研究该主题时,我在常规页面上发现了 THIS 表达式. 我意识到这篇文章中的代码正在使用PyQt4.但是我想让它与PyQt5一起工作,因为我已经用它开始了我的项目. (显然是盲目的-我只能为此找到C ++文档)

While researching the topic, I found THIS on Regular Expressions. I realize that the code in this post is using PyQt4. However I wanted to get this working with PyQt5, since I had already started my project with it. (Obviously blindly - I can only find C++ documentation for it)

这是我尝试过的:

# somewhere above:     
self.le_input = QtWidgets.QLineEdit()

# at some point validate_input gets called:
# more on that in the second part of this question

def validate_input(self):
    reg_ex = QtCore.QRegExp(""[0-9]+.?[0-9]{,2}"")
    input_validator = QtGui.QRegExpValidator(reg_ex, self.le_input.text())
    self.le_input.setValidator(input_validator)

运行代码时,出现以下错误:

When I run the code I get the following Error:

QRegExpValidator(父:QObject =无):参数1具有意外类型'QRegExp' QRegExpValidator(QRegExp,parent:QObject = None):参数2具有意外的类型'str'

QRegExpValidator(parent: QObject = None): argument 1 has unexpected type 'QRegExp' QRegExpValidator(QRegExp, parent: QObject = None): argument 2 has unexpected type 'str'

这些不是必需的参数吗? 有谁知道如何使它工作?

Aren't these exactly the required arguments? Does anyone know how to get this working?

常规:

在Python中使用PyQt实现实时验证的有效方法是什么?

What is an effective way to implement live validation with PyQt in Python?

此刻我将使用:

self.le_input.textChanged.connect(self.validate_input)

这确实有效,但是当我尝试将两个相互影响的QtLineEdits连接到同一插槽时,事情就停止了,因为两个人同时调用了"textChanged".

This does work, but as soon as I try to connect two QtLineEdits, that affect each other, to the same slot, things stop working because "textChanged" gets called by both of them at the same time.

用例:两个输入字段:Amount before TAXAmount after TAX-键入时,您输入的任何一个都会自动填充另一个字段.

Use case: Two input fields: Amount before TAX and Amount after TAX - and whichever you enter automatically fills the other one while typing.

首先进行验证,然后进行计算,然后输出到其他字段.

First validation, then calculation, then output to the other field.

非常感谢!我们非常感谢您的帮助!

推荐答案

您可以为不同的QLineEdit对象设置不同的验证器.

You can set different validators for different QLineEdit objects.

QRegExpValidator具有两个构造函数:

QRegExpValidator(parent: QObject = None)
QRegExpValidator(QRegExp, parent: QObject = None)

因此,您应该将代码更改为:

so, you should change your code to:

reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
input_validator = QRegExpValidator(reg_ex, self.le_input)
self.le_input.setValidator(input_validator)

一旦为QLineEdit设置了验证器,就无需使用

Once you set validator for QLineEdit, there is no need to use

self.le_input.textChanged.connect(self.validate_input)

只需删除它.那应该可以正常工作.

Just delete it. And that should work fine.

如果要查找有关PyQt5的文档,可以在控制台中简单地使用它:

If you want to find documentation about PyQt5, you can simple use this in your console:

pydoc3 PyQt5.QtGui.QRegExpValidator

但是pydoc只能向您显示很少的信息,因此您也应该使用C ++版本的文件记录.

But pydoc can only show you little information, so you should use C++ version documation as well.

最后,有关此的示例:

from PyQt5.Qt import QApplication
from PyQt5.QtCore import QRegExp
from PyQt5.QtGui import QRegExpValidator
from PyQt5.QtWidgets import QWidget, QLineEdit

import sys

class MyWidget(QWidget):
    def __init__(self, parent=None):
        super(QWidget, self).__init__(parent)
        self.le_input = QLineEdit(self)

        reg_ex = QRegExp("[0-9]+.?[0-9]{,2}")
        input_validator = QRegExpValidator(reg_ex, self.le_input)
        self.le_input.setValidator(input_validator)

if __name__ == '__main__':
    a = QApplication(sys.argv)

    w = MyWidget()
    w.show()

    a.exec()

这篇关于PyQt5和Python中的用户输入验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆