-
Notifications
You must be signed in to change notification settings - Fork 18
/
example1.py
executable file
·89 lines (68 loc) · 2.76 KB
/
example1.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#!/usr/bin/python
# Copyright (C) 2013 Patrick Totzke <patricktotzke@gmail.com>
# This file is released under the GNU GPL, version 3 or a later revision.
import urwid
from urwidtrees.tree import SimpleTree
from urwidtrees.widgets import TreeBox
# define some colours
palette = [
('body', 'black', 'light gray'),
('focus', 'light gray', 'dark blue', 'standout'),
('bars', 'dark blue', 'light gray', ''),
('arrowtip', 'light blue', 'light gray', ''),
('connectors', 'light red', 'light gray', ''),
]
# We use selectable Text widgets for our example..
class FocusableText(urwid.WidgetWrap):
"""Selectable Text used for nodes in our example"""
def __init__(self, txt):
t = urwid.Text(txt)
w = urwid.AttrMap(t, 'body', 'focus')
urwid.WidgetWrap.__init__(self, w)
def selectable(self):
return True
def keypress(self, size, key):
return key
# define a test tree in the format accepted by SimpleTree. Essentially, a
# tree is given as (nodewidget, [list, of, subtrees]). SimpleTree accepts
# lists of such trees.
def construct_example_simpletree_structure(selectable_nodes=True, children=3):
Text = FocusableText if selectable_nodes else urwid.Text
# define root node
tree = (Text('ROOT'), [])
# define some children
c = g = gg = 0 # counter
for i in range(children):
subtree = (Text('Child {0:d}'.format(c)), [])
# and grandchildren..
for j in range(children):
subsubtree = (Text('Grandchild {0:d}'.format(g)), [])
for k in range(children):
leaf = (Text('Grand Grandchild {0:d}'.format(gg)), None)
subsubtree[1].append(leaf)
gg += 1 # inc grand-grandchild counter
subtree[1].append(subsubtree)
g += 1 # inc grandchild counter
tree[1].append(subtree)
c += 1
return tree
def construct_example_tree(selectable_nodes=True, children=2):
# define a list of tree structures to be passed on to SimpleTree
forrest = [construct_example_simpletree_structure(selectable_nodes,
children)]
# stick out test tree into a SimpleTree and return
return SimpleTree(forrest)
def unhandled_input(k):
#exit on q
if k in ['q', 'Q']: raise urwid.ExitMainLoop()
if __name__ == "__main__":
# get example tree
stree = construct_example_tree()
# put the tree into a treebox
treebox = TreeBox(stree)
# add some decoration
rootwidget = urwid.AttrMap(treebox, 'body')
#add a text footer
footer = urwid.AttrMap(urwid.Text('Q to quit'), 'focus')
#enclose all in a frame
urwid.MainLoop(urwid.Frame(rootwidget, footer=footer), palette, unhandled_input = unhandled_input).run() # go