Python Forum
[Tkinter] Thousand Separator Entry Widget Cursor Pos Trouble
Thread Rating:
  • 1 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Thousand Separator Entry Widget Cursor Pos Trouble
#1
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()
Reply
#2
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
Reply
#3
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.
Reply
#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.
Reply
#5
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)
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ValueError: could not convert string to float: '' fron Entry Widget russellm44 5 497 Mar-06-2024, 08:42 PM
Last Post: russellm44
  [Tkinter] entry widget DPaul 5 1,437 Jul-28-2023, 02:31 PM
Last Post: deanhystad
  [Tkinter] Take the variable of a cursor class delcencen 2 1,150 Feb-13-2023, 05:19 AM
Last Post: deanhystad
  Tkinter Exit Code based on Entry Widget Nu2Python 6 2,877 Oct-21-2021, 03:01 PM
Last Post: Nu2Python
  method to add entries in multi columns entry frames in self widget sudeshna24 2 2,214 Feb-19-2021, 05:24 PM
Last Post: BashBedlam
  [Tkinter] Trouble getting data from an entry. Oshadha 2 2,436 Feb-05-2021, 03:35 AM
Last Post: steve_shambles
  Entry Widget issue PA3040 16 6,657 Jan-20-2021, 02:21 PM
Last Post: pitterbrayn
  [Tkinter] password with Entry widget TAREKYANGUI 9 5,778 Sep-24-2020, 05:27 PM
Last Post: TAREKYANGUI
  [Tkinter] Get the last entry in my text widget Pedroski55 3 6,295 Jul-13-2020, 10:34 PM
Last Post: Pedroski55
  How to retreive the grid location of an Entry widget kenwatts275 7 4,478 Apr-24-2020, 11:39 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020