The Entry Widget

Until now, the only input we knew how to get from the user was a mouseclick on a Button widget (Button, Checkbutton, or Radiobutton), which is handled via the -command option. Getting input from a mouseclick is useful, but it’s also limiting. The Entry widget (Figure 5-5) will let the user type in text that can then be used in any way by the application. Here are a few examples of where you might use an Entry widget:

  • In a database form that requires one entry per field (e.g., Name, Last name, Address)

  • In a software registration window that requires a serial number

  • In a login window that requires a username and password

  • In a configuration window to get the name of a printer

  • In an Open File window that requires the path and name of a file

Entry widget

Figure 5-5. Entry widget

Normally, we don’t care what users type in an Entry widget until they are done typing, and any processing will happen “after the fact” when a user clicks some sort of Go Button. You could get fancy and process each character as it’s typed by setting up a complicated bind, but it’s probably more trouble than it’s worth.

The user can type anything into an Entry widget. It is up to you to decide whether the text entered is valid or not. When preparing to use the information from an Entry, we should do some error checking. If we want an integer but get some alphabetic characters, we should issue a warning or error message ...

Get Mastering Perl/Tk now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.