elhacker.net cabecera Bienvenido(a), Visitante. Por favor Ingresar o Registrarse
¿Perdiste tu email de activación?.

 

 


Tema destacado: Trabajando con las ramas de git (tercera parte)


+  Foro de elhacker.net
|-+  Programación
| |-+  Scripting
| | |-+  Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
0 Usuarios y 1 Visitante están viendo este tema.
Páginas: [1] Ir Abajo Respuesta Imprimir
Autor Tema: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)  (Leído 2,414 veces)
El mas antiguo

Desconectado Desconectado

Mensajes: 94



Ver Perfil
Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
« en: 12 Febrero 2022, 22:03 pm »

Hola gente ¿Cómo están?, les quito un poquito de tiempo.-
La idea es que se pueda ingresar solo números, el signo ‘-’ y el punto ’.’ , el problema es que ingreso
-12500.23 todo bien pero x ej. si ingreso 125 y me posiciono al comienzo para el ingresar el ‘-’ no me funciona, ¿que estoy haciendo mal, que me falta?.   

Código
  1. from tkinter import ttk, messagebox
  2. import tkinter as tk    
  3.  
  4. def validar_entrada(previous_text, text, index):
  5.    if text.isdecimal():
  6.        return True
  7.    elif text == '.':
  8.        if text in previous_text:
  9.            return False
  10.        else:
  11.            return True
  12.    elif text == '-':
  13.        if len(previous_text) == 0:
  14.            return True
  15.        else:
  16.            if index == 0 and previous_text[0] != text:
  17.                return True
  18.            else:
  19.                return False
  20.    else:
  21.        return False    
  22.  
  23. root = tk.Tk()
  24. root.geometry("300x100+500+300")
  25. root.title("Unicamente decimales.")
  26.  
  27. ingreso = ttk.Entry(root, validate="key", validatecommand=(root.register(validar_entrada), "%s", "%S", "%i",))
  28. ingreso.place(x=10, y=20, width=150)
  29.  
  30. ingreso.focus()
  31.  

Slds. Daniel ☕☕☕


En línea

MCKSys Argentina
Moderador Global
***
Desconectado Desconectado

Mensajes: 5.470


Diviértete crackeando, que para eso estamos!


Ver Perfil
Re: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
« Respuesta #1 en: 13 Febrero 2022, 01:24 am »

Hola!

Revisa estas modificaciones:

Código
  1. from tkinter import ttk, messagebox
  2. import tkinter as tk
  3.  
  4. def validar_entrada(previous_text, text, index, action):
  5.    if text.isdigit():
  6.        if ((int(index) == 0) and (previous_text.startswith('-'))):
  7.            return False
  8.        else:
  9.            return True
  10.    elif text == '.':
  11.        if action == '0':
  12.            return True
  13.        else:
  14.            return previous_text.count(text) == 0
  15.    elif text == '-':
  16.        if action == '0':
  17.            return True
  18.        else:
  19.            if len(previous_text) == 0:
  20.                return True
  21.            else:
  22.                return (previous_text[0] != text) and (int(index) == 0)
  23.    else:
  24.        return False
  25.  
  26. def main():
  27.    root = tk.Tk()
  28.    root.geometry("300x100+500+300")
  29.    root.title("Unicamente decimales.")
  30.  
  31.    ingreso = ttk.Entry(root, validate="key", validatecommand=(root.register(validar_entrada), "%s", "%S", "%i", "%d"))
  32.    ingreso.place(x=10, y=20, width=150)
  33.  
  34.    ingreso.focus()
  35.    root.mainloop()
  36.  
  37.  
  38. if __name__ == '__main__':
  39.    main()
  40.  

Saludos!

PD: Seguro puede mejorarse mucho!


« Última modificación: 13 Febrero 2022, 01:31 am por MCKSys Argentina » En línea

MCKSys Argentina

"Si piensas que algo está bien sólo porque todo el mundo lo cree, no estás pensando."

El mas antiguo

Desconectado Desconectado

Mensajes: 94



Ver Perfil
Re: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
« Respuesta #2 en: 13 Febrero 2022, 14:01 pm »

Hola Argentina, en primer lugar decirte que funciona muy bien y que repase línea x línea y aprendí mucho de tu código, realmente gracias x tu tiempo.

Slds. Daniel ☕☕☕
En línea

Páginas: [1] Ir Arriba Respuesta Imprimir 

Ir a:  

WAP2 - Aviso Legal - Powered by SMF 1.1.21 | SMF © 2006-2008, Simple Machines