Recipe11.3.Using Default Values and Bounds with tkSimpleDialog Functions


Recipe 11.3. Using Default Values and Bounds with tkSimpleDialog Functions

Credit: Mike Foord, Peter Cogolo

Problem

You need to get an input value from the user with one of Tkinter's tkSimpleDialog dialog functions, but you want to add a default value, or to ensure that the value entered lies within certain bounds.

Solution

Each of Tkinter's tkSimpleDialog functions (askstring, askfloat, askinteger) supports an optional default value, as well as optional validation against minimum and maximum value. However, this set of features is not clearly spelled out in the documentation. Here's a wrapper function that you may find preferable:

import tkSimpleDialog _dispatch = { str: tkSimpleDialog.askstring,               int: tkSimpleDialog.askinteger,               float: tkSimpleDialog.askfloat,             } def getinput(title, prompt, type=str, default=None, min=None, max=None):     ''' gets from the user an input of type `type' (str, int or float),         optionally with a default value, and optionally constrained to         lie between the values `min' and `max' (included).     '''     f = _dispatch.get(type)     if not f:         raise TypeError, "Can't ask for %r input" % (type,)     return f(title, prompt, initialvalue=default, minvalue=min, maxvalue=max)

Discussion

The built-in tkSimpleDialog module offers a few simple functions that pop up dialogs that ask the user to input a string, a float, or an integernot a very advanced user interface but dirt-simple to use in your programs. Unfortunately, while these functions do support a few nice extras (the ability to pass in a default value, and having the result validated within certain optional minimum and maximum values), the module's documentation (what little there is of it) does not make this feature clear. Even the pydoc-generated page http://epydoc.sourceforge.net/stdlib/public/tkSimpleDialog-module.html just says "see SimpleDialog class." Since no such class exists, seeing it is not easy. (The relevant class is actually named _QueryDialog, and due to the leading underscore in the name, it is considered "private". Therefore pydoc does not build a documentation web page for it.)

This recipe shows how to access this functionality that's already part of the Python Standard Library. As a side benefit, it refactors the functionality into a single getinput function that takes as an argument the type of input desired (defaulting to str, meaning that the default type of result is a string, just as for built-in function raw_input). If you prefer the original concept of having three separate functions, it's easy to modify the recipe according to your tastes. The recipe mostly makes the semi-hidden functionality of the original functions' undocumented keyword arguments initialvalue, minvalue and maxvalue manifest and clearer through its optional parameters default, min, and max, which it passes right on to the underlying original function.

See Also

tkSimpleDialog module documentation is at http://epydoc.sourceforge.net/stdlib/public/tkSimpleDialog-module.html.



Python Cookbook
Python Cookbook
ISBN: 0596007973
EAN: 2147483647
Year: 2004
Pages: 420

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net