# Python interface to some of the commands of the 2.4 version of the
_busyCommand
= '::blt::busy'
_vectorCommand
= '::blt::vector'
_graphCommand
= '::blt::graph'
_testCommand
= '::blt::*'
_chartCommand
= '::blt::stripchart'
_tabsetCommand
= '::blt::tabset'
def _checkForBlt(window
):
# Blt may be a package which has not yet been loaded. Try to load it.
window
.tk
.call('package', 'require', 'BLT')
# Another way to try to dynamically load blt:
window
.tk
.call('load', '', 'Blt')
_haveBlt
= (window
.tk
.call('info', 'commands', _testCommand
) != '')
_haveBltBusy
= (window
.tk
.call('info', 'commands', _busyCommand
) != '')
window
= Tkinter
._default
_root
def busy_hold(window
, cursor
= None):
window
.tk
.call(_busyCommand
, 'hold', window
._w
)
window
.tk
.call(_busyCommand
, 'hold', window
._w
, '-cursor', cursor
)
def busy_release(window
):
window
.tk
.call(_busyCommand
, 'release', window
._w
)
window
.tk
.call(_busyCommand
, 'forget', window
._w
)
#=============================================================================
# Interface to the blt vector command which makes it look like the
# builtin python list type.
# The -variable, -command, -watchunset creation options are not supported.
# The dup, merge, notify, offset, populate, seq and variable methods
# and the +, -, * and / operations are not supported.
def vector_expr(expression
):
tk
= Tkinter
._default
_root
.tk
strList
= tk
.splitlist(tk
.call(_vectorCommand
, 'expr', expression
))
return tuple(map(string
.atof
, strList
))
def vector_names(pattern
= None):
tk
= Tkinter
._default
_root
.tk
return tk
.splitlist(tk
.call(_vectorCommand
, 'names', pattern
))
def __init__(self
, size
=None, master
=None):
# <size> can be either an integer size, or a string "first:last".
self
._master
= Tkinter
._default
_root
self
.tk
= self
._master
.tk
self
._name
= 'PY_VEC' + str(Vector
._varnum
)
Vector
._varnum
= Vector
._varnum
+ 1
self
.tk
.call(_vectorCommand
, 'create', self
._name
)
self
.tk
.call(_vectorCommand
, 'create', '%s(%s)' % (self
._name
, size
))
self
.tk
.call(_vectorCommand
, 'destroy', self
._name
)
return '[' + string
.join(map(str, self
), ', ') + ']'
return cmp(self
[:], list)
return self
.tk
.getint(self
.tk
.call(self
._name
, 'length'))
def __getitem__(self
, key
):
return self
.tk
.getdouble(self
.tk
.globalgetvar(self
._name
, str(key
)))
def __setitem__(self
, key
, value
):
return self
.tk
.globalsetvar(self
._name
, str(key
), float(value
))
def __delitem__(self
, key
):
return self
.tk
.globalunsetvar(self
._name
, str(key
))
def __getslice__(self
, start
, end
):
end
= end
- 1 # Blt vector slices include end point.
text
= self
.tk
.globalgetvar(self
._name
, str(start
) + ':' + str(end
))
return map(self
.tk
.getdouble
, self
.tk
.splitlist(text
))
def __setslice__(self
, start
, end
, list):
self
.set(self
[:start
] + list + self
[end
:])
def __delslice__(self
, start
, end
):
self
.set(self
[:start
] + self
[end
:])
def __radd__(self
, list):
# Python builtin list methods:
self
.tk
.call(self
._name
, 'append', args
)
return self
[:].count(obj
)
return self
[:].index(value
)
def insert(self
, index
, value
):
self
[index
:index
] = [value
]
del self
[self
.index(value
)]
# Blt vector instance methods:
# append - same as list method above
self
.tk
.call(self
._name
, 'clear')
self
.tk
.call((self
._name
, 'delete') + args
)
def expr(self
, expression
):
self
.tk
.call(self
._name
, 'expr', expression
)
def length(self
, newSize
=None):
return self
.tk
.getint(self
.tk
.call(self
._name
, 'length', newSize
))
def range(self
, first
, last
=None):
# Note that, unlike self[first:last], this includes the last
# item in the returned range.
text
= self
.tk
.call(self
._name
, 'range', first
, last
)
return map(self
.tk
.getdouble
, self
.tk
.splitlist(text
))
def search(self
, start
, end
=None):
return self
._master
._getints
(self
.tk
.call(
self
._name
, 'search', start
, end
))
if type(list) != types
.TupleType
:
self
.tk
.call(self
._name
, 'set', list)
# The blt vector sort method has different semantics to the python
# list sort method. Call these blt_sort:
def blt_sort(self
, *args
):
self
.tk
.call((self
._name
, 'sort') + args
)
def blt_sort_reverse(self
, *args
):
self
.tk
.call((self
._name
, 'sort', '-reverse') + args
)
# Special blt vector indexes:
return self
.tk
.getdouble(self
.tk
.globalgetvar(self
._name
, 'min'))
return self
.tk
.getdouble(self
.tk
.globalgetvar(self
._name
, 'max'))
# Method borrowed from Tkinter.Var class:
#=============================================================================
# This is a general purpose configure routine which can handle the
# configuration of widgets, items within widgets, etc. Supports the
# forms configure() and configure('font') for querying and
# configure(font = 'fixed', text = 'hello') for setting.
def _doConfigure(widget
, subcommand
, option
, kw
):
if not option
and not kw
:
# Return a description of all options.
options
= widget
.tk
.splitlist(widget
.tk
.call(subcommand
))
for optionString
in options
:
optionInfo
= widget
.tk
.splitlist(optionString
)
option
= optionInfo
[0][1:]
ret
[option
] = (option
,) + optionInfo
[1:]
# Return a description of the option given by <option>.
# Having keywords implies setting configuration options.
# Can't set and get in one command!
raise ValueError, 'cannot have option argument with keywords'
optionInfo
= widget
.tk
.splitlist(widget
.tk
.call(subcommand
+ (option
,)))
return (optionInfo
[0][1:],) + optionInfo
[1:]
# Otherwise, set the given configuration options.
widget
.tk
.call(subcommand
+ widget
._options
(kw
))
#=============================================================================
class Graph(Tkinter
.Widget
):
# Wrapper for the blt graph widget, version 2.4.
def __init__(self
, master
=None, cnf
={}, **kw
):
Tkinter
.Widget
.__init
__(self
, master
, _graphCommand
, cnf
, kw
)
def bar_create(self
, name
, **kw
):
self
.tk
.call((self
._w
, 'bar', 'create', name
) + self
._options
(kw
))
def line_create(self
, name
, **kw
):
self
.tk
.call((self
._w
, 'line', 'create', name
) + self
._options
(kw
))
return self
.tk
.getint(self
.tk
.call(self
._w
, 'extents', item
))
def invtransform(self
, winX
, winY
):
self
.tk
.call(self
._w
, 'invtransform', winX
, winY
))
return self
.tk
.getint(self
.tk
.call(self
._w
, 'inside', x
, y
))
def snap(self
, photoName
):
self
.tk
.call(self
._w
, 'snap', photoName
)
def transform(self
, x
, y
):
return self
._getdoubles
(self
.tk
.call(self
._w
, 'transform', x
, y
))
def axis_cget(self
, axisName
, key
):
return self
.tk
.call(self
._w
, 'axis', 'cget', axisName
, '-' + key
)
def axis_configure(self
, axes
, option
=None, **kw
):
# <axes> may be a list of axisNames.
if type(axes
) == types
.StringType
:
subcommand
= (self
._w
, 'axis', 'configure') + tuple(axes
)
return _doConfigure(self
, subcommand
, option
, kw
)
def axis_create(self
, axisName
, **kw
):
self
.tk
.call((self
._w
, 'axis', 'create', axisName
) + self
._options
(kw
))
def axis_delete(self
, *args
):
self
.tk
.call((self
._w
, 'axis', 'delete') + args
)
def axis_invtransform(self
, axisName
, value
):
return self
.tk
.getdouble(self
.tk
.call(
self
._w
, 'axis', 'invtransform', axisName
, value
))
def axis_limits(self
, axisName
):
return self
._getdoubles
(self
.tk
.call(
self
._w
, 'axis', 'limits', axisName
))
def axis_names(self
, *args
):
return self
.tk
.splitlist(
self
.tk
.call((self
._w
, 'axis', 'names') + args
))
def axis_transform(self
, axisName
, value
):
return self
.tk
.getint(self
.tk
.call(
self
._w
, 'axis', 'transform', axisName
, value
))
def xaxis_cget(self
, key
):
return self
.tk
.call(self
._w
, 'xaxis', 'cget', '-' + key
)
def xaxis_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'xaxis', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def xaxis_invtransform(self
, value
):
return self
.tk
.getdouble(self
.tk
.call(
self
._w
, 'xaxis', 'invtransform', value
))
return self
._getdoubles
(self
.tk
.call(self
._w
, 'xaxis', 'limits'))
def xaxis_transform(self
, value
):
return self
.tk
.getint(self
.tk
.call(
self
._w
, 'xaxis', 'transform', value
))
def xaxis_use(self
, axisName
= None):
return self
.tk
.call(self
._w
, 'xaxis', 'use', axisName
)
def x2axis_cget(self
, key
):
return self
.tk
.call(self
._w
, 'x2axis', 'cget', '-' + key
)
def x2axis_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'x2axis', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def x2axis_invtransform(self
, value
):
return self
.tk
.getdouble(self
.tk
.call(
self
._w
, 'x2axis', 'invtransform', value
))
return self
._getdoubles
(self
.tk
.call(self
._w
, 'x2axis', 'limits'))
def x2axis_transform(self
, value
):
return self
.tk
.getint(self
.tk
.call(
self
._w
, 'x2axis', 'transform', value
))
def x2axis_use(self
, axisName
= None):
return self
.tk
.call(self
._w
, 'x2axis', 'use', axisName
)
def yaxis_cget(self
, key
):
return self
.tk
.call(self
._w
, 'yaxis', 'cget', '-' + key
)
def yaxis_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'yaxis', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def yaxis_invtransform(self
, value
):
return self
.tk
.getdouble(self
.tk
.call(
self
._w
, 'yaxis', 'invtransform', value
))
return self
._getdoubles
(self
.tk
.call(self
._w
, 'yaxis', 'limits'))
def yaxis_transform(self
, value
):
return self
.tk
.getint(self
.tk
.call(
self
._w
, 'yaxis', 'transform', value
))
def yaxis_use(self
, axisName
= None):
return self
.tk
.call(self
._w
, 'yaxis', 'use', axisName
)
def y2axis_cget(self
, key
):
return self
.tk
.call(self
._w
, 'y2axis', 'cget', '-' + key
)
def y2axis_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'y2axis', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def y2axis_invtransform(self
, value
):
return self
.tk
.getdouble(self
.tk
.call(
self
._w
, 'y2axis', 'invtransform', value
))
return self
._getdoubles
(self
.tk
.call(self
._w
, 'y2axis', 'limits'))
def y2axis_transform(self
, value
):
return self
.tk
.getint(self
.tk
.call(
self
._w
, 'y2axis', 'transform', value
))
def y2axis_use(self
, axisName
= None):
return self
.tk
.call(self
._w
, 'y2axis', 'use', axisName
)
def crosshairs_cget(self
, key
):
return self
.tk
.call(self
._w
, 'crosshairs', 'cget', '-' + key
)
def crosshairs_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'crosshairs', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def crosshairs_off(self
):
self
.tk
.call(self
._w
, 'crosshairs', 'off')
self
.tk
.call(self
._w
, 'crosshairs', 'on')
def crosshairs_toggle(self
):
self
.tk
.call(self
._w
, 'crosshairs', 'toggle')
def element_activate(self
, name
, *args
):
self
.tk
.call((self
._w
, 'element', 'activate', name
) + args
)
def element_bind(self
, tagName
, sequence
=None, func
=None, add
=None):
return self
._bind
((self
._w
, 'element', 'bind', tagName
),
def element_unbind(self
, tagName
, sequence
, funcid
=None):
self
.tk
.call(self
._w
, 'element', 'bind', tagName
, sequence
, '')
self
.deletecommand(funcid
)
def element_cget(self
, name
, key
):
return self
.tk
.call(self
._w
, 'element', 'cget', name
, '-' + key
)
def element_closest(self
, x
, y
, *args
, **kw
):
success
= self
.tk
.getint(self
.tk
.call(
(self
._w
, 'element', 'closest', x
, y
, var
) +
self
._options
(kw
) + args
))
rtn
['dist'] = self
.tk
.getdouble(self
.tk
.globalgetvar(var
, 'dist'))
rtn
['x'] = self
.tk
.getdouble(self
.tk
.globalgetvar(var
, 'x'))
rtn
['y'] = self
.tk
.getdouble(self
.tk
.globalgetvar(var
, 'y'))
rtn
['index'] = self
.tk
.getint(self
.tk
.globalgetvar(var
, 'index'))
rtn
['name'] = self
.tk
.globalgetvar(var
, 'name')
def element_configure(self
, names
, option
=None, **kw
):
# <names> may be a list of elemNames.
if type(names
) == types
.StringType
:
subcommand
= (self
._w
, 'element', 'configure') + tuple(names
)
return _doConfigure(self
, subcommand
, option
, kw
)
def element_deactivate(self
, *args
):
self
.tk
.call((self
._w
, 'element', 'deactivate') + args
)
def element_delete(self
, *args
):
self
.tk
.call((self
._w
, 'element', 'delete') + args
)
def element_exists(self
, name
):
return self
.tk
.getboolean(
self
.tk
.call(self
._w
, 'element', 'exists', name
))
def element_names(self
, *args
):
return self
.tk
.splitlist(
self
.tk
.call((self
._w
, 'element', 'names') + args
))
def element_show(self
, nameList
=None):
nameList
= tuple(nameList
)
return self
.tk
.splitlist(
self
.tk
.call(self
._w
, 'element', 'show', nameList
))
def element_type(self
, name
):
return self
.tk
.call(self
._w
, 'element', 'type', name
)
def grid_cget(self
, key
):
return self
.tk
.call(self
._w
, 'grid', 'cget', '-' + key
)
def grid_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'grid', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
self
.tk
.call(self
._w
, 'grid', 'off')
self
.tk
.call(self
._w
, 'grid', 'on')
self
.tk
.call(self
._w
, 'grid', 'toggle')
def legend_activate(self
, *args
):
self
.tk
.call((self
._w
, 'legend', 'activate') + args
)
def legend_bind(self
, tagName
, sequence
=None, func
=None, add
=None):
return self
._bind
((self
._w
, 'legend', 'bind', tagName
),
def legend_unbind(self
, tagName
, sequence
, funcid
=None):
self
.tk
.call(self
._w
, 'legend', 'bind', tagName
, sequence
, '')
self
.deletecommand(funcid
)
def legend_cget(self
, key
):
return self
.tk
.call(self
._w
, 'legend', 'cget', '-' + key
)
def legend_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'legend', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def legend_deactivate(self
, *args
):
self
.tk
.call((self
._w
, 'legend', 'deactivate') + args
)
def legend_get(self
, pos
):
return self
.tk
.call(self
._w
, 'legend', 'get', pos
)
def pen_cget(self
, name
, key
):
return self
.tk
.call(self
._w
, 'pen', 'cget', name
, '-' + key
)
def pen_configure(self
, names
, option
=None, **kw
):
# <names> may be a list of penNames.
if type(names
) == types
.StringType
:
subcommand
= (self
._w
, 'pen', 'configure') + tuple(names
)
return _doConfigure(self
, subcommand
, option
, kw
)
def pen_create(self
, name
, **kw
):
self
.tk
.call((self
._w
, 'pen', 'create', name
) + self
._options
(kw
))
def pen_delete(self
, *args
):
self
.tk
.call((self
._w
, 'pen', 'delete') + args
)
def pen_names(self
, *args
):
return self
.tk
.splitlist(self
.tk
.call((self
._w
, 'pen', 'names') + args
))
def postscript_cget(self
, key
):
return self
.tk
.call(self
._w
, 'postscript', 'cget', '-' + key
)
def postscript_configure(self
, option
=None, **kw
):
subcommand
= (self
._w
, 'postscript', 'configure')
return _doConfigure(self
, subcommand
, option
, kw
)
def postscript_output(self
, fileName
=None, **kw
):
prefix
= (self
._w
, 'postscript', 'output')
return self
.tk
.call(prefix
+ self
._options
(kw
))
self
.tk
.call(prefix
+ (fileName
,) + self
._options
(kw
))
def marker_after(self
, first
, second
=None):
self
.tk
.call(self
._w
, 'marker', 'after', first
, second
)
def marker_before(self
, first
, second
=None):
self
.tk
.call(self
._w
, 'marker', 'before', first
, second
)
def marker_bind(self
, tagName
, sequence
=None, func
=None, add
=None):
return self
._bind
((self
._w
, 'marker', 'bind', tagName
),
def marker_unbind(self
, tagName
, sequence
, funcid
=None):
self
.tk
.call(self
._w
, 'marker', 'bind', tagName
, sequence
, '')
self
.deletecommand(funcid
)
def marker_cget(self
, name
, key
):
return self
.tk
.call(self
._w
, 'marker', 'cget', name
, '-' + key
)
def marker_configure(self
, names
, option
=None, **kw
):
# <names> may be a list of markerIds.
if type(names
) == types
.StringType
:
subcommand
= (self
._w
, 'marker', 'configure') + tuple(names
)
return _doConfigure(self
, subcommand
, option
, kw
)
def marker_create(self
, type, **kw
):
(self
._w
, 'marker', 'create', type) + self
._options
(kw
))
def marker_delete(self
, *args
):
self
.tk
.call((self
._w
, 'marker', 'delete') + args
)
def marker_exists(self
, name
):
return self
.tk
.getboolean(
self
.tk
.call(self
._w
, 'marker', 'exists', name
))
def marker_names(self
, *args
):
return self
.tk
.splitlist(
self
.tk
.call((self
._w
, 'marker', 'names') + args
))
def marker_type(self
, name
):
type = self
.tk
.call(self
._w
, 'marker', 'type', name
)
#=============================================================================
# Wrapper for the blt stripchart widget, version 2.4.
def __init__(self
, master
=None, cnf
={}, **kw
):
Tkinter
.Widget
.__init
__(self
, master
, _chartCommand
, cnf
, kw
)
#=============================================================================
class Tabset(Tkinter
.Widget
):
# Wrapper for the blt TabSet widget, version 2.4.
def __init__(self
, master
=None, cnf
={}, **kw
):
Tkinter
.Widget
.__init
__(self
, master
, _tabsetCommand
, cnf
, kw
)
def activate(self
, tabIndex
):
self
.tk
.call(self
._w
, 'activate', tabIndex
)
# This is the 'bind' sub-command:
def tag_bind(self
, tagName
, sequence
=None, func
=None, add
=None):
return self
._bind
((self
._w
, 'bind', tagName
), sequence
, func
, add
)
def tag_unbind(self
, tagName
, sequence
, funcid
=None):
self
.tk
.call(self
._w
, 'bind', tagName
, sequence
, '')
self
.deletecommand(funcid
)
def delete(self
, first
, last
= None):
self
.tk
.call(self
._w
, 'delete', first
, last
)
# This is the 'focus' sub-command:
def tab_focus(self
, tabIndex
):
self
.tk
.call(self
._w
, 'focus', tabIndex
)
return self
.tk
.call(self
._w
, 'get', tabIndex
)
def index(self
, tabIndex
):
index
= self
.tk
.call(self
._w
, 'index', tabIndex
)
return self
.tk
.getint(self
.tk
.call(self
._w
, 'index', tabIndex
))
def insert(self
, position
, name1
, *names
, **kw
):
(self
._w
, 'insert', position
, name1
) + names
+ self
._options
(kw
))
def invoke(self
, tabIndex
):
return self
.tk
.call(self
._w
, 'invoke', tabIndex
)
def move(self
, tabIndex1
, beforeOrAfter
, tabIndex2
):
self
.tk
.call(self
._w
, 'move', tabIndex1
, beforeOrAfter
, tabIndex2
)
return self
.tk
.call(self
._w
, 'nearest', x
, y
)
def scan_mark(self
, x
, y
):
self
.tk
.call(self
._w
, 'scan', 'mark', x
, y
)
def scan_dragto(self
, x
, y
):
self
.tk
.call(self
._w
, 'scan', 'dragto', x
, y
)
self
.tk
.call(self
._w
, 'see', index
)
self
.tk
.call(self
._w
,'see',tabIndex
)
return self
.tk
.getint(self
.tk
.call(self
._w
, 'size'))
def tab_cget(self
, tabIndex
, option
):
return self
.tk
.call(self
._w
, 'tab', 'cget', tabIndex
, option
)
def tab_configure(self
, tabIndexes
, option
=None, **kw
):
# <tabIndexes> may be a list of tabs.
if type(tabIndexes
) in (types
.StringType
, types
.IntType
):
tabIndexes
= [tabIndexes
]
subcommand
= (self
._w
, 'tab', 'configure') + tuple(tabIndexes
)
return _doConfigure(self
, subcommand
, option
, kw
)
def tab_names(self
, *args
):
return self
.tk
.splitlist(self
.tk
.call((self
._w
, 'tab', 'names') + args
))
def tab_tearoff(self
, tabIndex
, newName
= None):
name
= self
.tk
.call(self
._w
, 'tab', 'tearoff', tabIndex
)
return self
.nametowidget(name
)
self
.tk
.call(self
._w
, 'tab', 'tearoff', tabIndex
, newName
)
s
= self
.tk
.call(self
._w
, 'view')
return tuple(map(self
.tk
.getint
, self
.tk
.splitlist(s
)))
def view_moveto(self
, fraction
):
self
.tk
.call(self
._w
, 'view', 'moveto', fraction
)
def view_scroll(self
, number
, what
):
self
.tk
.call(self
._w
, 'view', 'scroll', number
, what
)