Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from tkinter import *
- from tkinter import ttk
- class MyFrame(Frame):
- def __init__(self, *args, **kwargs) -> None:
- super().__init__(*args, **kwargs)
- self.make_widgets()
- self.pack()
- def make_widgets(self):
- self.entvar = StringVar() # por si se usa en la aplicación
- # usamos ttk.Entry, porque no desactiva la validación en caso de modificar el entry progrmáticamente
- self.ent = ttk.Entry(self, validate='key', textvariable=self.entvar)
- vlcm = (self.register(self.add_dots), "%P")
- self.ent.config(validatecommand=vlcm)
- self.ent.pack()
- def add_dots(self, P):
- P = P.replace('.', '') # quitamos los puntos, nos quedamos solo con los dígitos
- if P.isdigit() or len(P) == 0:
- P = P[::-1] # damos vuelta el string
- dotted_string = '.'.join(P[i:i + 3] for i in range(0, len(P), 3))
- formatted_string = dotted_string[::-1] # volvemos a dar vuelt la cadena
- self.ent.delete(0, END)
- self.ent.insert(0, formatted_string)
- return True
- else:
- return False
- root = Tk()
- app = MyFrame(root)
- app.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment