Foro de elhacker.net

Programación => Scripting => Mensaje iniciado por: El mas antiguo en 12 Febrero 2022, 22:03 pm



Título: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
Publicado por: El mas antiguo 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 ☕☕☕


Título: Re: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
Publicado por: MCKSys Argentina 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!


Título: Re: Ingresar el signo ‘-’ en un caja de texto que contiene números.- (tkinter)
Publicado por: El mas antiguo 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 ☕☕☕