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?. from tkinter import ttk, messagebox import tkinter as tk def validar_entrada(previous_text, text, index): if text.isdecimal(): return True elif text == '.': if text in previous_text: return False else: return True elif text == '-': if len(previous_text) == 0: return True else: if index == 0 and previous_text[0] != text: return True else: return False else: return False root = tk.Tk() root.geometry("300x100+500+300") root.title("Unicamente decimales.") ingreso = ttk.Entry(root, validate="key", validatecommand=(root.register(validar_entrada), "%s", "%S", "%i",)) ingreso.place(x=10, y=20, width=150) ingreso.focus()
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: from tkinter import ttk, messagebox import tkinter as tk def validar_entrada(previous_text, text, index, action): if text.isdigit(): if ((int(index) == 0) and (previous_text.startswith('-'))): return False else: return True elif text == '.': if action == '0': return True else: return previous_text.count(text) == 0 elif text == '-': if action == '0': return True else: if len(previous_text) == 0: return True else: return (previous_text[0] != text) and (int(index) == 0) else: return False def main(): root = tk.Tk() root.geometry("300x100+500+300") root.title("Unicamente decimales.") ingreso = ttk.Entry(root, validate="key", validatecommand=(root.register(validar_entrada), "%s", "%S", "%i", "%d")) ingreso.place(x=10, y=20, width=150) ingreso.focus() root.mainloop() if __name__ == '__main__': main()
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 ☕☕☕
|