pythonware.com products ::: library ::: search ::: daily Python-URL!
   

Data Entry

Back   Next   

 Data Entry

The tkSimpleDialog module provides an interface to the following simple dialogs.

 Strings

The askstring function in the tkSimpleDialog 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.

tkSimpleDialog.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:

tkSimpleDialog.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.

tkSimpleDialog.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.

 File Names

The tkFileDialog module (included in the standard dialog kit described earlier) can be used to get a filename from the user. The module provides two convenience functions, one to get an existing filename so you can open it, and one to get a new filename, to save things into.

tkFileDialog.askopenfilename([options]). If the dialog is cancelled by the user, the function returns None.

tkFileDialog.asksaveasfilename([options]).

Figure 9-6. askopenfilename, asksaveasfilename

The following options can be used with the askopenfilename and asksavefilename functions:

Table 9-4. askopenfilename options

Option

Type

Description

defaultextension

string

An extension to add to the filename, if not explicitly given by the user. The string should include the leading dot (ignored by the open dialog).

filetypes

list

Sequence of (label, pattern) tuples. The same label may occur with several patterns. Use "*" as the pattern to indicate all files.

initialdir

string

Initial directory.

initialfile

string

Initial file (ignored by the open dialog)

parent

widget

Which window to place the message box on top of. When the dialog is closed, the focus is returned to the parent window.

title

string

Message box title.

 Colors

The tkColorChooser module (included in the standard dialog kit described earlier) can be used to specify an RGB color value.

tkColorChooser.askcolor([color [,options]]). The convenience function returns two values; the first is the color as a RGB triplet (a 3-tuple containing the red, green and blue values as integers in the range 0-255), the second a Tk color string. To preset a color when you display the dialog, you can pass a color (in either format) to the function.

If the dialog is cancelled, the function returns (None, None)

Figure 9-7. askcolor (in Swedish)

The following options can be used with the askcolor function:

Table 9-5. askcolor Options

Option

Type

Description

initialcolor

color

Color to mark as selected when dialog is displayed (given as an RGB triplet or a Tk color string). (the first argument to the convenience function).

parent

widget

Which window to place the message box on top of. When the dialog is closed, the focus is returned to the parent window.

title

string

Message box title.

Back   Next