Skip to content

Instantly share code, notes, and snippets.

@ejherran
Last active May 19, 2016 02:58
Show Gist options
  • Save ejherran/6cf309acc86b71733de1f51e48265f4d to your computer and use it in GitHub Desktop.
Save ejherran/6cf309acc86b71733de1f51e48265f4d to your computer and use it in GitHub Desktop.
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
class Test(QtWidgets.QWidget):
def __init__(self):
super(Test, self).__init__()
self.setObjectName("self")
self.resize(400, 41)
self.lineEdit = QtWidgets.QLineEdit(self)
self.lineEdit.setGeometry(QtCore.QRect(10, 10, 381, 22))
self.lineEdit.setObjectName("lineEdit")
# Vincular el cambio de texto del campo
self.lineEdit.textChanged.connect(self.validator)
self.retranslateUi()
QtCore.QMetaObject.connectSlotsByName(self)
def validator(self):
pos = '0123456789.-'
t = self.sender().text()
if(len(t) > 0):
# Caracteres posibles
if(t[-1] not in pos):
t = t[:-1]
if(len(t) > 0):
# Validar un solo - al inicio
if( (t[-1] == '-' and '-' in t[:-1]) or (t[-1] == '-' and len(t) > 1) ):
t = t[:-1]
if(len(t) > 0):
# Validar un solo .
if(t[-1] == '.' and '.' in t[:-1]):
t = t[:-1]
self.sender().setText(t)
def retranslateUi(self):
_translate = QtCore.QCoreApplication.translate
self.setWindowTitle(_translate("self", "self"))
app = QtWidgets.QApplication(sys.argv)
t = Test()
t.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment