Formatting widget data

Input data such as date, time, phone number, credit card number, website URL, and IP number, have an associated display format. For instance, the date can be better represented in a MM/DD/YYYY format.

Fortunately, it is easy to format data in the required format as the user enters it in the widget, as shown in the following screenshot:

The 10.07_formatting_entry_widget_to_display_date.py code automatically formats user input to insert forward slashes at the required places to display the date entered by a user in the MM/DD/YYYY format:

from tkinter import Tk, Entry, Label, StringVar, INSERT

class FormatEntryWidgetDemo:

def __init__(self, root):
Label(root, text='Date(MM/DD/YYYY)').pack()
self.entered_date = StringVar()
self.date_entry = Entry(textvariable=self.entered_date)
self.date_entry.pack(padx=5, pady=5)
self.date_entry.focus_set()
self.slash_positions = [2, 5]
root.bind('<Key>', self.format_date_entry_widget)

def format_date_entry_widget(self, event):
entry_list = [c for c in self.entered_date.get() if c !='/']
for pos in self.slash_positions:
if len(entry_list) > pos:
entry_list.insert(pos, '/')
self.entered_date.set(''.join(entry_list))
# Controlling cursor
cursor_position = self.date_entry.index(INSERT) # current cursor
position
for pos in self.slash_positions:
if cursor_position == (pos + 1): # if cursor position is on slash
cursor_position += 1
if event.keysym not in ['BackSpace', 'Right', 'Left','Up', 'Down']:
self.date_entry.icursor(cursor_position)

root = Tk()
FormatEntryWidgetDemo(root)
root.mainloop()

The description of the preceding code is as follows:

  • The Entry widget is bound to the keypress event, where every new keypress calls the related format_date_entry_widget callback method.
  • First, the format_date_entry_widget method breaks down the entered text into an equivalent list named entry_list and ignores the slash/symbol that may have been entered by a user.
  • It then iterates through the self.slash_positions list and inserts the slash symbol at all the required positions in entry_list. The net result of this is a list that has slashes inserted at all the right places.
  • The next line converts this list into an equivalent string using join() and then sets the value of the Entry widget to this string. This ensures that the Entry widget text is formatted into the aforementioned date format.
  • The remaining pieces of code simply control the cursor to ensure that the cursor advances by one position whenever it encounters the slash symbol. It also ensures that keypresses, such as Backspace, right, left, up, and down are handled properly.

Note that this method does not validate the date value, and users may add an invalid date. The method defined here will simply format it by adding a forward slash at the third and sixth positions. Adding date validation to this example is left as an exercise for you to complete.

This concludes our brief discussion on formatting data within widgets. Hopefully, you should now be able to create formatted widgets for a wide variety of input data that can be better displayed in a given format.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.224.56.29