Data Entry
The tkinter.simpledialog module provides an interface to the following simple dialogs.
Strings
The askstring function in the tkinter.simpledialog module prompts the user for a string. You specify the dialog title and the prompt string, and the function returns when the user closes the dialog. The prompt string may include newline characters.
tkinter.simpledialog.askstring(title, prompt [,options]). Ask the user to enter an string value. If the user pressed Enter, or clicked OK, the function returns the string. If the user closed the dialog by pressing Escape, clicking Cancel, or explicitly via the window manager, this function returns None.
Figure 9-4. askstring
The following options can be used with this function:
Table 9-2. askstring Options
Option |
Type |
Description |
---|---|---|
initialvalue |
string |
Initial value, if any. Default is an empty string. |
parent |
widget |
Which window to place the dialog on top of. When the dialog is closed, the focus is returned to the parent window. |
Numeric Values
The askinteger and askfloat functions is similar to askstring, but they only accept integers and float values, respectively. You can also use the minvalue and maxvalue options to limit the input range:
tkinter.simpledialog.askinteger(title, prompt [,options]). Ask the user to enter an integer value. If the entered value is not a valid integer or floating point value, a message box is displayed, and the dialog is not closed. As with the askstring function, the function returns None if the dialog box is cancelled.
tkinter.simpledialog.askfloat(title, prompt [,options]). Same, but returns a floating point value.
Figure 9-5. askinteger, askfloat
The following options can be used with these functions:
Table 9-3. askinteger and askfloat options
Option |
Type |
Description |
---|---|---|
initialvalue |
integer or float |
Initial value, if any. Default is an empty string. |
parent |
widget |
Which window to place the dialog on top of. When the dialog is closed, the focus is returned to the parent window. |
minvalue |
integer or float |
Minimum value. If exceeded, a message box is shown when the user clicks OK, and the dialog will not be closed. Default is no check. |
maxvalue |
integer or float |
Maximum value. If exceeded, a message box is shown when the user clicks OK, and the dialog will not be closed. Default is no check. |