[Python] Criando um Entry formatado como moeda em Python (Tkinter)

Discussão em 'Python' iniciado por Stack, Setembro 9, 2024.

  1. Stack

    Stack Membro Participativo

    Gostaria de saber como seria a aplicação de uma formatação em um campo Entry, utilizando o tkinter, para o formato de valor com casas decimais e pontuação de milhar, no caso, 000.000.000,00.Tentei adaptar um código parecido para CPF que inclusive esta aqui no stackoverflow mas sem sucesso. Grato.

    Segue código do CPF


    text = entry.get().replace(".", "").replace("-", "")[:11]
    new_text = ""

    if event.keysym.lower() == "backspace": return

    for index in range(len(text)):

    if not text[index] in "0123456789": continue
    if index in [2, 5]: new_text += text[index] + "."
    elif index == 8: new_text += text[index] + "-"
    else: new_text += text[index]

    entry.delete(0, "end")
    entry.insert(0, new_text)


    from tkinter import *
    screen = Tk()

    entry = Entry(screen, font = ("Arial", 20))
    entry.bind("<KeyRelease>", format_cpf)
    entry.pack()
    screen.mainloop()

    Continue reading...

Compartilhe esta Página