程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何使 Pyqt5 LineEdit 只接受数字大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何使 Pyqt5 LineEdit 只接受数字?

开发过程中遇到如何使 Pyqt5 LineEdit 只接受数字的问题如何解决?下面主要结合日常开发的经验,给出你关于如何使 Pyqt5 LineEdit 只接受数字的解决方法建议,希望对你解决如何使 Pyqt5 LineEdit 只接受数字有所启发或帮助;

目前,我正在使用 python 和 PYQT5 开发一个程序,其中用户输入只能是数字。问题是我不知道该怎么做。例如当我得到这个变量

VAR_1=float(self.ui.lineEdit.text())

我需要输入的文本只是一个数字。我的意思是当用户试图写一个字母 o 符号时什么也没有发生。

解决方法

使用/尝试 SETVALidator@H_673_15@ 或 seTinputMask@H_673_15@ 方法

import sys

from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *

class ButtonName(QWidget):
    def __init__(self):
        super().__init__()
        self.setWindowtitle("Example - Validator")
        self.setGeometry(800,200,200)
        self.UI()
        self.layouts()
        self.show()

    def UI(self):
        self.lbl_Integer = QLabel("Integer Validator")
        self.textbox_Integervalidator = QLineEdit()
        self.textbox_Integervalidator.setPlaceholderText("upto 3 digit value only accept")
        self.textbox_Integervalidator.SETVALidator(QIntValidator(1,999,self))

        self.lbl_double = QLabel("Double Validator")
        self.textbox_doublevalidator = QLineEdit()
        self.textbox_doublevalidator.SETVALidator(QDoubleValidator(0.99,99.99,2))

        self.lbl_regexp = QLabel("RexExp Validator")
        self.textbox_regexpvalidator = QLineEdit()
        reg_ex_1 = QRegExp("[0-9]+.?[0-9]{,2}") # double
        # reg_ex_2 = QRegExp("[0-9]{1,5}")  # minimum 1 Integer number to maxiumu 5 Integer number
        # reg_ex_3 = QRegExp("-?\\d{1,3}")  # accept negative number also
        # reg_ex_4 = QRegExp("")
        self.textbox_regexpvalidator.SETVALidator(QRegExpValidator(reg_ex_1))

    def layouts(self):
        mainlayout = QVBoxLayout()
        mainlayout.addWidget(self.lbl_Integer)
        mainlayout.addWidget(self.textbox_Integervalidator)
        mainlayout.addWidget(self.lbl_doublE)
        mainlayout.addWidget(self.textbox_doublevalidator)
        mainlayout.addWidget(self.lbl_regexp)
        mainlayout.addWidget(self.textbox_regexpvalidator)
        self.setLayout(mainlayout)

def main():
    app = QApplication(sys.argv)
    mainwindow = ButtonName()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

大佬总结

以上是大佬教程为你收集整理的如何使 Pyqt5 LineEdit 只接受数字全部内容,希望文章能够帮你解决如何使 Pyqt5 LineEdit 只接受数字所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。