Ornn
Ornn

Reputation: 15

Python/Tkinter - change the "." through the ","

I am developing an application for calculating taxes on revenue, the code itself works normally, but i would like to know if there is a way to change the "." by "," when typing in the entry fields.

Example: 100,50 instead of 100.50

Follow the code below:

from tkinter import *
# ---
root = Tk()
root.geometry('350x350')
# ---
l_receita1 = Label(root, text='Receita 1')
l_receita1.place(x=10, y=10)
e_receita1 = Entry(root)
e_receita1.place(x=100, y=10)
l_receita2 = Label(root, text='Receita 2')
l_receita2.place(x=10, y=40)
e_receita2 = Entry(root)
e_receita2.place(x=100, y=40)
# ---
v_result1 = DoubleVar()
l_resRec1 = Label(root, textvariable=v_result1)
l_resRec1.place(x=10, y=100)
v_result2 = DoubleVar()
l_resRec2 = Label(root, textvariable=v_result2)
l_resRec2.place(x=10, y=140)
v_result3 = DoubleVar()
l_resRec3 = Label(root, textvariable=v_result3)
l_resRec3.place(x=10, y=220)
# ---
def calc():
    v_result1.set(round(float(e_receita1.get()) * 8 / 100, 2))
    v_result2.set(round(float(e_receita2.get()) * 12 / 100, 2))
    v_result3.set(round(float(v_result1.get() + v_result2.get()), 2))

    e_receita1.delete(0, END)
    e_receita2.delete(0, END)
# ---
bt = Button(root, text='Calcular', command=calc)
bt.place(x=10, y=180)
# ---
root.mainloop()

Upvotes: 1

Views: 47

Answers (2)

tgikal
tgikal

Reputation: 1680

Using a bind, and in the callback function replacing "." with ",":

from tkinter import *
# ---
root = Tk()
root.geometry('350x350')
# ---
def callback(e):
    """Function to change "." to "," while typing in an entry"""
    val = e.widget.get()

    # If statement avoids unnecessary delete/insert calls
    if "." in val:
        e.widget.delete(0, "end")
        e.widget.insert(0, val.replace(".", ","))

l_receita1 = Label(root, text='Receita 1')
l_receita1.place(x=10, y=10)
e_receita1 = Entry(root)
e_receita1.bind('<KeyRelease>', callback) # Bind the key release
e_receita1.place(x=100, y=10)
l_receita2 = Label(root, text='Receita 2')
l_receita2.place(x=10, y=40)
e_receita2 = Entry(root)
e_receita2.bind('<KeyRelease>', callback) # Bind the key release
e_receita2.place(x=100, y=40)
# ---
v_result1 = DoubleVar()
l_resRec1 = Label(root, textvariable=v_result1)
l_resRec1.place(x=10, y=100)
v_result2 = DoubleVar()
l_resRec2 = Label(root, textvariable=v_result2)
l_resRec2.place(x=10, y=140)
v_result3 = DoubleVar()
l_resRec3 = Label(root, textvariable=v_result3)
l_resRec3.place(x=10, y=220)
# ---
def calc():
    v_result1.set(round(float(e_receita1.get().replace(",", ".")) * 8 / 100, 2))
    v_result2.set(round(float(e_receita2.get().replace(",", ".")) * 12 / 100, 2))
    v_result3.set(round(float(v_result1.get() + v_result2.get()), 2))

    e_receita1.delete(0, END)
    e_receita2.delete(0, END)
# ---
bt = Button(root, text='Calcular', command=calc)
bt.place(x=10, y=180)
# ---
root.mainloop()

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385900

You can bind to the "." character and have it insert a "," instead. Use return "break" to prevent the default behavior.

def replace_period(event):
    event.widget.insert("insert", ",")
    return "break"

e_receita1.bind("<.>", replace_period)  # or "<period>"

Upvotes: 1

Related Questions