user71666
user71666

Reputation: 40

Forcing formatted text in entry widget

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?

Upvotes: 0

Views: 2076

Answers (1)

Bryan Oakley
Bryan Oakley

Reputation: 385900

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.

Upvotes: 0

Related Questions