Javier
Javier

Reputation: 25

How to make that Pyqt5 LineEdit accepts only numbers

Currently, I am working on a program with python and PYQT5 where user inputs have to be only numbers. The problem is that I have no idea how to do it. For example when I get this variable

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

I need that the text entered is only a number. i mean when the user is trying to write a letter o symbol nothing happens.

Upvotes: 1

Views: 8874

Answers (3)

AHMED dz
AHMED dz

Reputation: 36

You can try it like this

self.ui.lineEdit.textChanged.connect(self.accept_only_numbers)

def accept_only_numbers(self): 
    VAR_1=float(self.ui.lineEdit.text()) 
    if  VAR_1.isnumeric() == True:
        print("ok is number") 
    else:
        VAR_1.clear()

Upvotes: 1

Gustavo mota
Gustavo mota

Reputation: 49

try make a mask for your line edit:

self.lineEdit.setInputMask("00")

When you put "0" in Mask, the code will understand that it can only be numbers. The amount of zero placed will be the amount of houses that the lineedit will accept. In this case how and "00" the lineEdit accepted two houses (the ten's houses)

Upvotes: 2

Kumar
Kumar

Reputation: 653

Use/Try setValidator or setInputMask methods

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

Upvotes: 3

Related Questions