scitools.TkGUI

Module with functions and classes used in the GUI chapters of the book “Python Scripting for Computational Science”.

class scitools.TkGUI.AutoSimVizCGI[source]

Organize a set of form variables for input data.

Methods

footer() Write out HTML footer instructions.
make(form, parameters, CGI_script[, imagefile]) Create an HTML page consisting of an optional
__init__()[source]
__module__ = 'scitools.TkGUI'
footer()[source]

Write out HTML footer instructions.

make(form, parameters, CGI_script, imagefile=None)[source]

Create an HTML page consisting of an optional image (specified by imagefile), a table of form variables (specified by parameters (scitools.ParameterInterface.Parameters)), and a “simulate and visualize” button. The resulting visualization part must be created after calling this function. Finally, the HTML page needs a footer (see the footer function).

class scitools.TkGUI.AutoSimVizGUI[source]

Organize a set of widgets for input data together with buttons for running a simulator and performing visualizations. The input data are represented by a Parameters object from the ParameterInterface module. The individual parameters in this object are represented as InputPrmGUI instances. The application code creates Parameters object (recall to call addend() after all parameters are registered).

The method make_prmGUI takes the Parameters objects, makes the associated widgets and packs them in an appropriate GUI. All widgets may appear in one column, in the order the parameters were registered in the Parameters object, if sort_widgets is false. Otherwise, two column of widgets are made: one with sliders and one with the rest (checkbuttons, entries, options). The sequence of widgets in the latter case is determined by the sequence of registration in the Parameters, e.g., all sliders are grouped in their original sequence, all option menus are grouped in their original sequence, and so on.

The method make_buttonGUI creates buttons for simulation and visualization, plus an optional logo and a help button. If more buttons are wanted, one can add these to the button_frame Tkinter.Frame attribute.

There is an optional column of widgets with BLT graphs for curve plotting, enabled by the make_curveplotGUI method.

The great advantage of this class is that the application code can concentrate on defining input parameters to a problem, the simulation and visualization functions, and leave it to this class to put everything together. It is then an easy task to change the layout of the whole GUI in one common place.

Methods

load_curveplot(filename, graph[, curvename]) Load data from a two-column file into x and y Blt vectors.
make_buttonGUI(parent[, buttons, logo, help])
make_curveplotGUI(parent[, ...]) @param parent: parent (master) widget
make_prmGUI(parent, parameters[, ...]) The height parameter controls the height (in pixels) of
update_curveplot(filename, graph) Update Blt vectors with data from a two-column file.
__init__()[source]
__module__ = 'scitools.TkGUI'
load_curveplot(filename, graph, curvename='')[source]

Load data from a two-column file into x and y Blt vectors. graph is a Pmw.Blt.Graph widget, normally returned from make_curveplotGUI.

x, y = self.someGUI.load_curveplot(‘my.dat’, self.plot2,
curvename=’measured data’)

One can convert x and y, which are plain Python lists, to NumPy arrays for further processing if desired.

make_buttonGUI(parent, buttons=[], logo=None, help=None)[source]
make_curveplotGUI(parent, no_of_plotframes=1, placement='right')[source]

@param parent: parent (master) widget @param no_of_plotframes: no of graph areas @param placement: placement of the plot area (‘right’ or ‘bottom’)

Example on creating three plot areas to the right in the window:

self.plot1, self.plot2, self.plot3 =               self.someGUI.make_curveplotGUI(parent, 3, 'right')
self.plot1 etc. holds Pmw.Blt.Graph widgets.
Create a single plot area::
self.plot1 = self.someGUI.make_curveplotGUI(parent,
1, ‘bottom’)
make_prmGUI(parent, parameters, sort_widgets=0, height=None, pane=0)[source]

The height parameter controls the height (in pixels) of the GUI.

The columns are realized by Pmw.ScrolledFrame widgets.

update_curveplot(filename, graph)[source]

Update Blt vectors with data from a two-column file.

class scitools.TkGUI.CanvasCoords[source]

Utilities for transforming between canvas coordinates and physical (real) coordinates.

Methods

c2p(x, y) Inverse of physical2canvas.
c2p4(coords) Inverse of physical2canvas4.
canvas2physical(x, y) Inverse of physical2canvas.
canvas2physical4(coords) Inverse of physical2canvas4.
cx(x) Transform physical x to canvas x.
cy(y) Transform physical y to canvas y.
p2c(x, y) Transform physical (x,y) to canvas 2-tuple.
p2c4(coords) Transform physical 4-tuple (x1,x2,y1,y2) to
physical2canvas(x, y) Transform physical (x,y) to canvas 2-tuple.
physical2canvas4(coords) Transform physical 4-tuple (x1,x2,y1,y2) to
print_coordinate_system()
scale(dx) Transform a length in canvas coordinates
set_coordinate_system(canvas_width, ...[, ...]) Define parameters in the physical coordinate system (origin, width) expressed in canvas coordinates.
__init__()[source]
__module__ = 'scitools.TkGUI'
c2p(x, y)

Inverse of physical2canvas.

c2p4(coords)

Inverse of physical2canvas4.

canvas2physical(x, y)[source]

Inverse of physical2canvas.

canvas2physical4(coords)[source]

Inverse of physical2canvas4.

cx(x)[source]

Transform physical x to canvas x.

cy(y)[source]

Transform physical y to canvas y.

p2c(x, y)

Transform physical (x,y) to canvas 2-tuple.

p2c4(coords)

Transform physical 4-tuple (x1,x2,y1,y2) to canvas 4-tuple.

physical2canvas(x, y)[source]

Transform physical (x,y) to canvas 2-tuple.

physical2canvas4(coords)[source]

Transform physical 4-tuple (x1,x2,y1,y2) to canvas 4-tuple.

print_coordinate_system()[source]
scale(dx)[source]

Transform a length in canvas coordinates to a length in physical coordinates.

set_coordinate_system(canvas_width, canvas_height, x_origin, y_origin, x_range=1.0)[source]

Define parameters in the physical coordinate system (origin, width) expressed in canvas coordinates. x_range is the width of canvas window in physical coordinates.

class scitools.TkGUI.DrawFunction(xcoor, parent, ymin=0.0, ymax=1.0, width=500, height=200, curvename=' ', ylabel='', xlabel='', curvecolor='green', curvewidth=4, yrange_widgets=True)[source]

Interactive drawing of y=f(x) functions. The drawing takes place in a Pmw.Blt.Graph widget.

Methods

configure(**kwargs) Legal parameters (kwargs):
erase() delete all curves and make new empty self.x and self.y
get() return points (x,y), interpolated to the grid, where
interpolate()
mouse_down(event)
mouse_drag(event)
mouse_up(event)
pack(**kwargs)
set_yaxis(ymin, ymax)
__init__(xcoor, parent, ymin=0.0, ymax=1.0, width=500, height=200, curvename=' ', ylabel='', xlabel='', curvecolor='green', curvewidth=4, yrange_widgets=True)[source]

Interactive drawing of a function.

xcoor grid points (on the x axsis) for interpolation parent parent widget ymin, ymax initial extent of the y axis width, height size of widget curvename name of function to be drawn xlabel, ylabel labels on the axis curvecolor color of the drawn curve curvewidth line thickness of the drawn curve yrange_widgets True: add text entries for range of y axis

These parameters, except for parent and yrange_widgets, can also be set as keyword arguments in the configure method.

__module__ = 'scitools.TkGUI'
configure(**kwargs)[source]

Legal parameters (kwargs):

xcoor grid points (on the x axsis) for interpolation width, height size of widget curvename name of function to be drawn xlabel, ylabel labels on the axis curvecolor color of the drawn curve curvewidth line thickness of the drawn curve

ymin and ymax are set in set_yaxis method.

erase()[source]

delete all curves and make new empty self.x and self.y

get()[source]

return points (x,y), interpolated to the grid, where x and y are NumPy arrays of coordinates

interpolate()[source]
mouse_down(event)[source]
mouse_drag(event)[source]
mouse_up(event)[source]
pack(**kwargs)[source]
set_yaxis(ymin, ymax)[source]
class scitools.TkGUI.DrawFunctionDialog(xcoor, parent=None)[source]

Methods

action(result)
get()
__init__(xcoor, parent=None)[source]

Dialog box with DrawFunction widget

__module__ = 'scitools.TkGUI'
action(result)[source]
get()[source]
class scitools.TkGUI.Drawing(parent, func_spec)[source]

Bases: scitools.TkGUI.UserFunction

Methods

get() Return function object.
__init__(parent, func_spec)[source]
__module__ = 'scitools.TkGUI'
get()[source]

Return function object.

class scitools.TkGUI.FuncDependenceViz(master, parameter_intervals={}, functions={}, xmin=0.0, xmax=1.0, resolution=101, width=500, height=400, viztool='Pmw.Blt.Graph', plot_update='after')[source]

Visualization of the shape of a function depends continuously on its parameters, and this class makes a graphical illustration of this dependence.

Methods

bind_vectors2BLTgraph() bind vectors to the curves in the BLT graph
fill_vectors()
make_vectors() make x vector and a dictionary of y vectors
psdump([event])
quit([event]) kill plot window
visualize(var)
__init__(master, parameter_intervals={}, functions={}, xmin=0.0, xmax=1.0, resolution=101, width=500, height=400, viztool='Pmw.Blt.Graph', plot_update='after')[source]

Define a set of functions depending on a set of parameters. This class creates a GUI where the parameters can be adjusted, and the effect on the function graphs can be seen immediately.

__module__ = 'scitools.TkGUI'
bind_vectors2BLTgraph()[source]

bind vectors to the curves in the BLT graph

fill_vectors()[source]
make_vectors()[source]

make x vector and a dictionary of y vectors

psdump(event=None)[source]
quit(event=None)[source]

kill plot window

visualize(var)[source]
class scitools.TkGUI.FuncSpec(representation, name='', parameters=None, independent_variables=[], formula=None, image=None, function_object=None, vector=0, description=None, xcoor=None, scrolled_frame=False)[source]

Specification of a function. Lists of such specifications can be fed to class FunctionSelector to form a notebook where each page is designed according to the contents of a FuncSpec object.

Methods

configure(**kwargs)
get_independent_variables()
ok()
__init__(representation, name='', parameters=None, independent_variables=[], formula=None, image=None, function_object=None, vector=0, description=None, xcoor=None, scrolled_frame=False)[source]

Arguments:

@param representation: class Drawing, UserFunction, or
StringFormula

@param name: name of function @param parameters: parameters in the function, either

dict or Parameters instance
@param independent_variables: list/tuple of strings with the
names of the indep. variables.

@param formula: textual doc of function formula @param image: filename of GIF image (LaTeX) @param function_object: callable object for evaluating the function @param vector: 0: scalar function, >0: no of vector comp. @param description: more verbose description than formula @param xcoor: array of coordinates for drawing @param scrolled_frame: scrollbars in the notebook page, False

or dict: {‘width’: 300, ‘height’:200}

Examples: see test_FunctionSelector in TkGUI.py.

__module__ = 'scitools.TkGUI'
__repr__()[source]
configure(**kwargs)[source]
get_independent_variables()[source]
ok()[source]
class scitools.TkGUI.FunctionChoices(parent, func_spec_list)[source]

Notebook for various representations of a function. The representations appear as pages. Each page is realized as a UserFunction, StringFormula, or Drawing instance.

Methods

get() Return initialized function object corresponding to
__init__(parent, func_spec_list)[source]
__module__ = 'scitools.TkGUI'
get()[source]

Return initialized function object corresponding to the currently selected notebook page.

class scitools.TkGUI.FunctionSelector(parent)[source]

Notebook with a collection of functions to be specified. Each function is represented by a FunctionChoices page. This page is again a notebook with pages corresponding to different ways of specifying a function: drawing, string formulas, ready-made formulas with free parameters, hardcoded Python functions etc.

Methods

add(name, func_spec_list)
get(name) Return initialized function object corresponding to
pack(**kwargs)
select(name, page) Select page under the name tab.
__init__(parent)[source]
__module__ = 'scitools.TkGUI'
add(name, func_spec_list)[source]
get(name)[source]

Return initialized function object corresponding to the page with the given name.

pack(**kwargs)[source]
select(name, page)[source]

Select page under the name tab.

class scitools.TkGUI.InputPrm(name=None, default=0.0, str2type=None, help=None, unit=None, cmlarg=None, prmclass=None)[source]

Class for holding data about a parameter.

Methods

get() Return the value of the parameter.
getPhysicalQuantity()
get_wunit() Return value with unit (dimension) as string, if it has.
set(value) Set the value of the parameter.
__init__(name=None, default=0.0, str2type=None, help=None, unit=None, cmlarg=None, prmclass=None)[source]

default default value str2type string to type conversion

(float, int, str, str2bool)

name parameter name help description of parameter unit physical unit (dimension) cmlarg command-line argument for sending

this prm to an external program
prmclass classification of this parameter, e.g.,
‘numerics’, ‘physics’, ‘material’, etc.

Note: value with unit only works if str is float or int

>>> p=InputPrm('q', 1, float, unit='m')
>>> p.set(6)
>>> p.get()
6.0
>>> p.set('6 cm')
>>> p.get()
0.059999999999999998
>>> p=InputPrm('q', '1 m', float, unit='m')
>>> p.set('1 km')
>>> p.get()
1000.0
>>> p.get_wunit()
'1000.0 m'
>>> p.unit
'm'
__module__ = 'scitools.TkGUI'
__repr__()[source]

Application of eval to this output creates the instance.

__str__()[source]

Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.

get()[source]

Return the value of the parameter.

getPhysicalQuantity()[source]
get_wunit()[source]

Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).

set(value)[source]

Set the value of the parameter.

v

value of parameter

class scitools.TkGUI.InputPrmCGI(name=None, default=0.0, str2type=None, widget_type='entry', values=None, form=None, help=None, unit=None, cmlarg=None)[source]

Bases: scitools.TkGUI.InputPrm

Represent a parameter by a form variable in HTML.

Methods

get()
getPhysicalQuantity()
get_wunit() Return value with unit (dimension) as string, if it has.
make_form_entry() Write the form’s input field, according to widget_type.
set(value) Set the value of the parameter.
__init__(name=None, default=0.0, str2type=None, widget_type='entry', values=None, form=None, help=None, unit=None, cmlarg=None)[source]

default default value str2type function from string to type name name of parameter widget_type entry, slider, option, checkbutton values option values form cgi.FieldStorage object help description of parameter unit physical unit (dimension) cmlarg command-line argument for sending

this prm to an external program
__module__ = 'scitools.TkGUI'
__repr__()[source]

Application of eval to this output creates the object.

__str__()

Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.

get()[source]
getPhysicalQuantity()
get_wunit()

Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).

make_form_entry()[source]

Write the form’s input field, according to widget_type.

set(value)

Set the value of the parameter.

v

value of parameter

class scitools.TkGUI.InputPrmGUI(name=None, default=0.0, str2type=None, widget_type='entry', values=None, parent=None, help=None, unit=None, cmlarg=None)[source]

Bases: scitools.TkGUI.InputPrm

Represent an input parameter by a widget.

Methods

get() Get GUI text/number, handle special input like numbers
getPhysicalQuantity()
get_widget_type()
get_wunit() Return value with unit (dimension) as string, if it has.
make_GUI_variable_Tk(str2type, unit, name) Bind self._v to a variable with set and get methods for
make_widget()
make_widget_Tk() Make Tk widget according to self._widget_type.
set(value)
GUI_toolkit = 'Tkinter/Pmw'
__init__(name=None, default=0.0, str2type=None, widget_type='entry', values=None, parent=None, help=None, unit=None, cmlarg=None)[source]

@param default: default value @param str2type: function from string to type @param name: name of parameter @param widget_type: entry, slider, option, checkbutton @param values: (min,max) interval or options @param parent: parent widget @param help: description of parameter @param unit: physical unit (dimension) @param cmlarg: command-line argument for sending

this prm to an external program
__module__ = 'scitools.TkGUI'
__repr__()[source]

Application of eval to this output creates the object.

__str__()

Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.

get()[source]

Get GUI text/number, handle special input like numbers with units, if necessary.

getPhysicalQuantity()
get_widget_type()[source]
get_wunit()

Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).

make_GUI_variable_Tk(str2type, unit, name)[source]

Bind self._v to a variable with set and get methods for setting and getting the value in/from a GUI.

make_widget()[source]
make_widget_Tk()[source]

Make Tk widget according to self._widget_type.

set(value)[source]
v

value of parameter

widget_type
class scitools.TkGUI.Parameters(interface='plain', form=None, prm_dict={})[source]

Class for holding a set of InputPrm-type parameters. See src/py/examples/simviz/simviz1cp.py for examples on usage.

Some attributes may be useful in application code:

self.dict is a dictionary of InputPrm-type objects.

self.parameters_sequence (and self._seq) is a list of InputPrm-type objects in the sequence they were registered.

self.sliders_sequence is a list of InputPrm-type objects, with slider widget representation in a GUI, in the sequence they were registered. self.entries_sequence, self.checkbt_sequence, self.options_sequence are similar for text entries, checkbuttons, and option menus.

The self.*_sequence lists can be used to build GUIs or CGI scripts. Normally, this is automated in classes like AutoSimVizGUI and AutoSimVizCGI.

Methods

add(name, default[, str2type, widget_type, ...]) Add a new parameter.
dump()
endadd() Process parameters, make internal data structures.
get() Return dictionary with (name,value) pairs.
keys() Return parameter names.
name2attr() Turn all item keys into attributes.
parse_options(argv) Examine the command line and for each -opt val pair, set the value of parameter opt to val, if opt is a registered parameter.
usage() Print legal command-line options.
__getitem__(name)[source]
__init__(interface='plain', form=None, prm_dict={})[source]

@param interface: ‘plain’, ‘CGI’, or ‘GUI’ @param form: cgi.FieldStorage() object @param prm_dict: dictionary with (name,value) pairs (will be added using the add method)

__iter__()[source]

Iterate over keys in self.dict.

__module__ = 'scitools.TkGUI'
__repr__()[source]
__setattr__(name, value)[source]

If name2attr is called, self.m = 2.3 (using this function) is safe, because this also implies update of the corresponding InputPrm-type object in self.dict.

__setitem__(name, value)[source]
__str__()[source]
add(name, default, str2type=None, widget_type='entry', values=None, help=None, unit=None, cmlarg=None)[source]

Add a new parameter.

dump()[source]
endadd()[source]

Process parameters, make internal data structures.

get()[source]

Return dictionary with (name,value) pairs.

keys()[source]

Return parameter names. With this method Parameter objects p can be used in dictionary update functions: somedict.update(p).

name2attr()[source]

Turn all item keys into attributes. Warning: values are copied! __setitem__ and __setattr__ (or properties) must take care of parallel updates.

parse_options(argv)[source]

Examine the command line and for each -opt val pair, set the value of parameter opt to val, if opt is a registered parameter. argv is typically sys.argv[1:] Note that the name of a parameter may contain blanks. A blank is replaced by two underscores in the command-line options.

usage()[source]

Print legal command-line options.

class scitools.TkGUI.StringFormula(parent, func_spec)[source]

Methods

get() Return function object.
__init__(parent, func_spec)[source]
__module__ = 'scitools.TkGUI'
get()[source]

Return function object.

class scitools.TkGUI.UserFunction(parent, func_spec)[source]

Methods

get() Return function object.
__init__(parent, func_spec)[source]
__module__ = 'scitools.TkGUI'
get()[source]

Return function object.

scitools.TkGUI.commandline2dict(argv, parameters)[source]

Load data from the command line into a dictionary of parameter values. The argv argument is typically sys.argv[1:]. Each option –opt in argv is extracted and the proceeding value v is assigned to parameters:

parameters[opt].set(v)

Hence, parameters must hold objects that have a set function. Normally, parameters is a dictionary of InputPrm objects.

scitools.TkGUI.createInputPrm(interface, name, default, str2type=None, widget_type='entry', values=None, parent=None, form=None, help=None, unit=None, cmlarg=None)[source]

Unified interface to parameter classes InputPrm/GUI/CGI.

scitools.TkGUI.parametersGUI(p, parent, pack_side='top', scrolled={'width': 350, 'height': 400})[source]

Load all parameters in a Parameters object p into a GUI.

parent parent widget pack_side packing is donw with

widget.pack(side=pack_side, expand=1, fill=’both’)
scrolled False: use standard Tk Frame
non-empty dict: use Pmw.ScrolledFrame with the prescribed height and width
scitools.TkGUI.points2grid(x, y, xcoor)[source]

Transform points (x,y) to a uniform grid with coordinates xcoor.

scitools.TkGUI.roundInt(a)[source]
scitools.TkGUI.test_FunctionChoices(root)[source]

This Page