Module with functions and classes used in the GUI chapters of the book “Python Scripting for Computational Science”.
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 |
Write out HTML footer instructions.
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).
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. |
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.
One can convert x and y, which are plain Python lists, to NumPy arrays for further processing if desired.
@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.
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. |
Inverse of physical2canvas.
Inverse of physical2canvas4.
Transform physical (x,y) to canvas 2-tuple.
Transform physical 4-tuple (x1,x2,y1,y2) to canvas 4-tuple.
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) |
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.
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.
Methods
action(result) | |
get() |
Bases: scitools.TkGUI.UserFunction
Methods
get() | Return function object. |
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) |
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.
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() |
Arguments:
@param name: name of function @param parameters: parameters in the function, either
dict or Parameters instance
@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.
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 |
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. |
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. |
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
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'
Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.
Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).
value of parameter
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. |
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
Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.
Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).
Set the value of the parameter.
value of parameter
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) |
@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
Compact output; just the value as a formatted string. Note that __str__ is used by __repr__ so strings must be enclosed in quotes.
Return value with unit (dimension) as string, if it has. Otherwise, return value (with the right type).
Bind self._v to a variable with set and get methods for setting and getting the value in/from a GUI.
value of parameter
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. |
@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)
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.
Add a new parameter.
Return parameter names. With this method Parameter objects p can be used in dictionary update functions: somedict.update(p).
Turn all item keys into attributes. Warning: values are copied! __setitem__ and __setattr__ (or properties) must take care of parallel updates.
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.
Methods
get() | Return function object. |
Methods
get() | Return function object. |
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.
Unified interface to parameter classes InputPrm/GUI/CGI.
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’)