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
90
91
92
|
#!/usr/bin/env python
"""
A simple example of a few buttons and click handlers.
"""
from prompt_toolkit.application import Application
from prompt_toolkit.application.current import get_app
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
from prompt_toolkit.layout import HSplit, Layout, VSplit
from prompt_toolkit.styles import Style
from prompt_toolkit.widgets import Box, Button, Frame, Label, TextArea
# Event handlers for all the buttons.
def button1_clicked():
text_area.text = "Button 1 clicked"
def button2_clicked():
text_area.text = "Button 2 clicked"
def button3_clicked():
text_area.text = "Button 3 clicked"
def exit_clicked():
get_app().exit()
# All the widgets for the UI.
button1 = Button("Button 1", handler=button1_clicked)
button2 = Button("Button 2", handler=button2_clicked)
button3 = Button("Button 3", handler=button3_clicked)
button4 = Button("Exit", handler=exit_clicked)
text_area = TextArea(focusable=True)
# Combine all the widgets in a UI.
# The `Box` object ensures that padding will be inserted around the containing
# widget. It adapts automatically, unless an explicit `padding` amount is given.
root_container = Box(
HSplit(
[
Label(text="Press `Tab` to move the focus."),
VSplit(
[
Box(
body=HSplit([button1, button2, button3, button4], padding=1),
padding=1,
style="class:left-pane",
),
Box(body=Frame(text_area), padding=1, style="class:right-pane"),
]
),
]
),
)
layout = Layout(container=root_container, focused_element=button1)
# Key bindings.
kb = KeyBindings()
kb.add("tab")(focus_next)
kb.add("s-tab")(focus_previous)
# Styling.
style = Style(
[
("left-pane", "bg:#888800 #000000"),
("right-pane", "bg:#00aa00 #000000"),
("button", "#000000"),
("button-arrow", "#000000"),
("button focused", "bg:#ff0000"),
("text-area focused", "bg:#ff0000"),
]
)
# Build a main application object.
application = Application(layout=layout, key_bindings=kb, style=style, full_screen=True)
def main():
application.run()
if __name__ == "__main__":
main()
|