Is there a way to "force" the format of text in a tkinter (or any) entry widget? For example, some forms online, when entering in your phone number, come pre-formatted with (___)___-____
and only allow 10 number entries. Is there a way to do this in python?
Asked
Active
Viewed 1,869 times
0
-
Almost exactly the same question was asked [two days ago](http://stackoverflow.com/q/30022059/3714930), I gave an answer using `validatecommand` there. Of course, as Bryan Oakley mentions, there are several options, depending on the exact behavior that you want. – fhdrsdg May 06 '15 at 16:11
-
Thank you, I am going to read into it! – user71666 May 06 '15 at 16:31
1 Answers
0
There is nothing built-in, but the entry widget has enough functionality to allow you to build it yourself.
For example, if the template is "xxx-xxxx", you can set a binding that automatically inserts a dash when three characters are detected, and you can also set up input validation that only allows numbers in the first three positions.
You can see a simple example in an article published by packt publishing. See the article Miscellaneous Tips, and search for "Formatting widget data"
A simpler solution is to use three entry widgets along with labels for the parts that are required.

Bryan Oakley
- 370,779
- 53
- 539
- 685
-
I appreciate it. I am concerned with the approach, I like to know how things are done more than is there built in functionality. – user71666 May 06 '15 at 16:33