python - 在 Tkinter Entry 中使用逗号分隔

标签 python tkinter

在我的 GUI 中,我有很多输入框。它们大多数用于大数字,我想使用逗号分隔。使用我的代码我可以这样做,但光标会自动向右移动。当自动插入逗号时。如何保持这个光标固定在最右边,或者有什么建议改变我的代码。

from tkinter import *
import locale

root = Tk()

a = StringVar()
b = Entry(root, textvariable = a, justify = RIGHT).pack()

def secod(*events):
    ray = a.get()
    raym = ray.replace(",",'')
    raymond = int(raym)
    try:
        asd = format(raymond,',')
    except:
        print("b")
    a.set(asd)

a.trace('w',secod)
root.mainloop()

附上问题图片。 enter image description here

最佳答案

问题是修改输入框的文本变量不会自动更新其光标位置。相反,您可以执行以下操作:

首先,将输入框的创建与其对齐方式分开(请参阅 here ):

b = Entry(root, textvariable = a, justify = RIGHT)
b.pack()

然后在观察者回调中,使用小部件自己的方法修改 Entrybox 的内容:

def secod(*events):
    ray = a.get()
    raym = ray.replace(",",'')
    raymond = int(raym)
    try:
        asd = format(raymond,',')
    except:
        print("b")
    # Overwrite the Entrybox content using the widget's own methods
    b.delete(0, END)
    b.insert(0, asd)

关于python - 在 Tkinter Entry 中使用逗号分隔,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44084596/

相关文章:

python-3.x - Tkinter 模态窗口不允许主窗口最大化

绑定(bind)到 Messagebox 的 Python PyInstaller Tkinter 按钮什么也不做

python - 读取txt文件的内容并在Tkinter GUI Python中显示

python - 无法通过 ftplib (Python) 使用 IP 地址

python - Tensorflow特征值分解极慢

python - 删除 "not"之后的第一个单词

python - 从列表创建多个复选框并获取所有值

python - 在 python 中执行查询

Python 访问错误的 key

python - 替代 tkinter.dooneevent