简体   繁体   English

Python/Tkinter - 更改“.” 通过“,”

[英]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示例:100,50 而不是 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()

You can bind to the "."您可以绑定到“。” character and have it insert a "," instead.字符并让它插入一个“,”。 Use return "break" to prevent the default behavior.使用return "break"来防止默认行为。

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

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

Using a bind , and in the callback function replacing "."使用bind ,并在回调函数中替换“.” 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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM