1

In my GUI, I have a lot of Entry boxes. Most of them are used for large numbers and I want to use comma separation. Using my code I am able to do so but the cursor moves right automatically. When a comma is inserted automatically. How to keep this cursor fixed at the extreme right, or there is any suggestion to change my code.

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()

The picture of the problem is attached with. enter image description here

Zeryab Hassan Kiani
  • 467
  • 1
  • 7
  • 18

1 Answers1

1

The problem is that modifying the Entrybox's textvariable doesn't automatically update its cursor position. Instead you could do the following:

First, separate your Entry box creation from its alignment (see here):

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

Then in your observer callback, modify the content of the Entrybox using the widget's own methods:

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)
Community
  • 1
  • 1
Josselin
  • 2,593
  • 2
  • 22
  • 35