Python Forum

Full Version: Thousand Separator Entry Widget Cursor Pos Trouble
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
how to fix cursor pos?

def atPos(atX=0,atY=0):
   frame = Frame(root)
   frame.place(x=atX,y=atY)
   return frame

class MyEntry:
   def __init__(self,parent):
       self.frame = Frame(parent, width=150,height=24)
       self.frame.pack_propagate(False)
       self.frame.pack()
       self.var = StringVar()
       self.var.trace("w", self.thousandseparator)
       self.vcmd = (self.frame.register(self.onValidate),'%d', '%P', '%S')
       self.entry = Entry(self.frame,textvariable=self.var, validate="all", validatecommand=self.vcmd)
       self.entry.pack(fill=BOTH,expand=True)

   def onValidate(self, d, P, S):
       if d=='1':
           if S in '0123456789':
               return True
           else:
               return False
       else:
           return True

   def thousandseparator(self,*arg):
       self.pra = self.var.get()
       self.buff = self.pra.replace(',','')
       if len(self.buff) > 0:
           self.value = int(self.buff)
           self.change = "{:,}".format(self.value)
           self.var.set(self.change)

if __name__ == '__main__':
   import locale
   from tkinter import *

   root = Tk()
   root.geometry('325x120+50+50')

   pos10 = atPos(15,10)
   label1 = Label(pos10,text='Type Number Only').pack()
   pos11 = atPos(150, 10)
   entry = MyEntry(pos11)

   pos20 = atPos(15,37)
   label2 = Label(pos20,text='Type Number again').pack()
   pos21 = atPos(150, 37)
   entry = MyEntry(pos21)

   pos3 = atPos(150,80)
   Exit = Button(pos3,text=' Exit ',command=lambda : quit()).pack()

   root.mainloop()
Would you give more details.
By fix do you mean repair, or stationary.
Aside from that, Does your window require a stationary location of the screen?
Just wondering why you use the most inflexible geometry 'place' rather than grid
when we typing number at that entry, var trace will set variable to thousand separator form but when it reach at thousand, million or billion cursor pos not at right positions. example: when type in 123 cursor place after number 3, but when we type 1234 its change to 1.234 cursor place before number 4 not after 4.
That's annoying.
This seems to be about that issue, perhaps the solution is here: https://www.reddit.com/r/learnpython/com...ble_trace/
or perhaps someone else in the groups knows the answer, sorry, I do not.
I do not know the answer for Tkinter. What if you just use StringVar() without the thousands separator.

You can handle the conversion outside with locale or with a third party module.

import locale


locale.setlocale(locale.LC_ALL, 'de_DE.UTF-8')
float_type = locale.delocalize('12.123.123,75')
locale_float_str = locale.format('%.2f', 12222222.34, grouping=True)