Convenience class for building Tk widgets trees from a dict or a JSON file
Project description
TkWidgetTree
============
Build trees of Tk widgets from a dict structure or use the FileParser class
to load them from a JSON file.
Details
-------
TkWidgetTree is a wrapper for the various Tk widgets (Frames, Buttons, Menu:s,
etc.), it is most conveninetly invoked with the TkWidgetTree.from_dict(tree,
root) static method, it builds a tree structure of TkWidgetTree objects from
nested dicts with the input to the TkWidgetTree constructor.
The parameters to the constructor are:
name - The Tk widget class to create or the name of the widget
parent* - The Tk widget that will be used as the parent
(tk.Button(parent, ...)
id* - A tree-unique name for this widget, for when you want to
find it for setting callbacks or fetching values (or whatever)
options* - A dict containing the options passed to the widget creation
(tk.Button(parent, **options))
pack* - A dict containing the options to give to the pack method. This
class only supports the pack arrangement method for the
moment.
children* - A list of child TkWidgetTree:s. The widgets in this list will
get this widget set as the parent when render()ing.
[*] Optional
You can then run the render() method on the root of your tree and they will
be recursively created. Once you've rendred all the widgets, you can set up
your callbacks by first finding your buttons/sliders whatnot by calling the
find method and using the TkWidgetTree.widget parameter to further set up
the widgets.
...
config={
'name': 'Frame',
'children': [
{
'id': 'mybutton',
'name': 'Button',
'options': { 'text': 'Hello World' },
'pack: { 'side': tk.LEFT }
}
]
}
tree = TkWidgetTree.from_dict(config)
tree.render()
button = tree.find('mybutton')
button.widget.config(callback=do_stuff)
tree.mainloop()
...
Loading from a file
-------------------
But keeping a chunky dict of your application in the source is still pretty
clunky, so why not put the structure in a file instead? That's where the
FileParser class comes in. It essentially reads a JSON-serialized version of
the above structure, with one small twist; any Tk constants you might want to
use are set as strings, with the key prefixed by an @-sign.
... "pack": { "@side": "LEFT" } ...
============
Build trees of Tk widgets from a dict structure or use the FileParser class
to load them from a JSON file.
Details
-------
TkWidgetTree is a wrapper for the various Tk widgets (Frames, Buttons, Menu:s,
etc.), it is most conveninetly invoked with the TkWidgetTree.from_dict(tree,
root) static method, it builds a tree structure of TkWidgetTree objects from
nested dicts with the input to the TkWidgetTree constructor.
The parameters to the constructor are:
name - The Tk widget class to create or the name of the widget
parent* - The Tk widget that will be used as the parent
(tk.Button(parent, ...)
id* - A tree-unique name for this widget, for when you want to
find it for setting callbacks or fetching values (or whatever)
options* - A dict containing the options passed to the widget creation
(tk.Button(parent, **options))
pack* - A dict containing the options to give to the pack method. This
class only supports the pack arrangement method for the
moment.
children* - A list of child TkWidgetTree:s. The widgets in this list will
get this widget set as the parent when render()ing.
[*] Optional
You can then run the render() method on the root of your tree and they will
be recursively created. Once you've rendred all the widgets, you can set up
your callbacks by first finding your buttons/sliders whatnot by calling the
find method and using the TkWidgetTree.widget parameter to further set up
the widgets.
...
config={
'name': 'Frame',
'children': [
{
'id': 'mybutton',
'name': 'Button',
'options': { 'text': 'Hello World' },
'pack: { 'side': tk.LEFT }
}
]
}
tree = TkWidgetTree.from_dict(config)
tree.render()
button = tree.find('mybutton')
button.widget.config(callback=do_stuff)
tree.mainloop()
...
Loading from a file
-------------------
But keeping a chunky dict of your application in the source is still pretty
clunky, so why not put the structure in a file instead? That's where the
FileParser class comes in. It essentially reads a JSON-serialized version of
the above structure, with one small twist; any Tk constants you might want to
use are set as strings, with the key prefixed by an @-sign.
... "pack": { "@side": "LEFT" } ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
tkwidgettree-1.0.0b2.tar.gz
(3.6 kB
view details)
File details
Details for the file tkwidgettree-1.0.0b2.tar.gz
.
File metadata
- Download URL: tkwidgettree-1.0.0b2.tar.gz
- Upload date:
- Size: 3.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | a96d37492f3ce7b40986fd3573055867604196ca06e508d580c2713d12034317 |
|
MD5 | 1a23069b2c9ae39239906bf90c29830e |
|
BLAKE2b-256 | bf0fbadf789a3976fbcf01de456b53af452d43c4d4d9282a939b15e401cecb31 |