0

I'm trying to dynamically format data entered within a tk.Entry. I know you can hook up a StringVar to the widget for access and bind an event to it but I'm unable to format the entry properly. In this case I'm trying to format and entered phone number while I'm typing. I want brackets around the area code, space before the next 3 numbers and a dash before the last 4.

(999) 999-9999

The binding with the event does work and I'm able to access the entered character but placement within the widget dynamically just doesn't work for me.

Python Code

def fmatphone(event):
    fphone = "(" + landline.get()
    landline.set(fphone)

landline = StringVar()

phonelentry = tk.Entry(
    framebody,
    justisfy="left",
    bg="#DCDCDC",
    fg="black",
    width=13,
    textvariable=landline)

phonelentry.grid(row=6, column=0, padx=250, pady=4, sticky="w")
phonelentry.bind("<Key>", fmatphone)

When I enter a 9 in phoneLentry the entry shows up a 9( instead of the reverse. Kind of like a timing issue or something. I might be going about this all wrong so please feel free to set me straight.

Woodford
  • 3,746
  • 1
  • 15
  • 29
weave
  • 1
  • 1
  • Does this answer your question? [Auto add dashes to entryboxes in tkinter](https://stackoverflow.com/questions/62119551/auto-add-dashes-to-entryboxes-in-tkinter) – Delrius Euphoria Jan 25 '23 at 17:50
  • Thanks for the input ... I did take a look but it appeared rather lengthy and passed on adopting. I did find a keyboard controlling module (pynput) and it worked perfectly for me. In my function I call the keyboard controller and manually (through code) press the backspace key. Using the Global StringVar for the Widget presented some sort of timing issue with the keyboard buffer and the variable. – weave Jan 26 '23 at 13:17

1 Answers1

0
import pynput.keyboard

def fmatphone(event):

    key_pressed = event.keysym

    if key_pressed not in "0123456789":

        keyboard = Controller()
        keyboard.press(pynput.keyboard.Key.backspace)
        keyboard.release(pynput.keyboard.Key.backspace)

        keyboard = None

phonelentry = tk.Entry(
    framebody,
    justisfy="left",
    bg="#DCDCDC",
    fg="black",
    width=13,
    textvariable=landline)

phonelentry.grid(row=6, column=0, padx=250, pady=4, sticky="w")
phonelentry.bind("<Key>", fmatphone)
acw1668
  • 40,144
  • 5
  • 22
  • 34
weave
  • 1
  • 1
  • Darn..... there was more to than the above.... but the it should be clear enough. – weave Jan 26 '23 at 13:26
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jan 27 '23 at 17:54
  • My timing issue was solved by changing the event binding to . Then I was able to apply all the editing I wanted on the input string. The event looks to fire before your input in the widget is complete which interfered with the validation I was attempting. – weave Jan 29 '23 at 11:11