在我的 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()
最佳答案
问题是修改输入框的文本变量不会自动更新其光标位置。相反,您可以执行以下操作:
首先,将输入框的创建与其对齐方式分开(请参阅 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/