python输入数据pyqt5_PyQt5和Python中的用户输入验证

1586010002-jmsa.png

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

The specific:

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)

This is what I tried:

# 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(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?

The general:

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

At the moment I would use:

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

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.

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.

Many thanks in advance! Any help is highly appreciated!

解决方案

You can set different validators for different QLineEdit objects.

QRegExpValidator has two constructors:

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)

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.

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

pydoc3 PyQt5.QtGui.QRegExpValidator

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

Finally, an example about this:

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()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值