5. Utilities¶
5.1. GUI Application Helper¶
TkBuilder GUI Application with a window title and the default witkets style.
One typical usage is to inherit the Application class and implement solution-specific methods.
A minimalist example without inheritance is given below:
>>> import witkets as wtk
>>> app = wtk.Application(ui_filename='demo.xml', title='Demo')
>>> mainwindow = app.root # tk.Tk instance
>>> builder = app.builder # TkBuilder instance
>>> app.run() # call app.root.mainloop()
- class witkets.application.Application(ui_filename=None, title=None, theme=None, **kw)¶
A GUI Application.
- Parameters
ui_filename (str) – path to the XML used by TkBuilder
- Keyword Arguments
title (str) – window title
theme (str) – theme name
kw (dict) – Any of the
tk.Tk
keyword arguments
- run()¶
Enter the application event loop.
5.2. Coordinate Normalization¶
Coordinate conversion utility classes.
This module provides the classes CoordConverter
and
CoordSys2D
. The former is used for a single axis, while the latter is
used for 2-dimension cartesian coordinates.
Upon construction, these classes receives arguments defining the axes bounds, expressed in world coordinates, and whether the axes are inverted.
Then, one can use the methods to convert to and from Normalized Device Coordinates [0..1].
Example:
>>> import witkets as wtk
>>> coord_sys = CoordSys2D(10, 20, -30, -10, y_inverted=True)
>>> x, y = 15, -10
>>> xn, yn = coord_sys.to_ndc(x, y)
>>> x2, y2 = coord_sys.from_ndc(xn, yn)
>>> print(' Original Coords: %s' % ([x, y]))
Original Coords: [15, -10]
>>> print('Normalized Coords: [%.1f, %.1f]' % (xn, yn))
Normalized Coords: [0.5, 0.0]
>>> print(' Loopback Coords: %s' % ([x2, y2]))
Loopback Coords: [15.0, -10.0]
- class witkets.coordsys.CoordConverter(minval, maxval, inverted=False)¶
Coordinate conversions to/from Normalized Device Coordinates.
- from_ndc(an)¶
Get coordinate from normalized device coordinates [0..1]
- in_range(a)¶
Check if value is within the bounds for this coordinate system.
- property inverted¶
Whether axis direction is inverted (read-only).
- property maxval¶
Maximum value (read-only).
- property minval¶
Minimum value (read-only).
- to_ndc(a)¶
Convert coordinate to normalized device coordinate [0..1]
- class witkets.coordsys.CoordSys2D(x_min, x_max, y_min, y_max, x_inverted=False, y_inverted=False)¶
Pair of coordinate converters for X and Y
- from_ndc(xn, yn)¶
Get coordinates from normalized device coordinates [0..1]
- in_range(x, y)¶
Check if value is within the range.
- to_ndc(x, y)¶
Convert a point to normalized device coordinates [0..1]