1#!/usr/bin/env python
2"""
3A simple example of a few buttons and click handlers.
4"""
5from prompt_toolkit.application import Application
6from prompt_toolkit.application.current import get_app
7from prompt_toolkit.key_binding import KeyBindings
8from prompt_toolkit.key_binding.bindings.focus import focus_next, focus_previous
9from prompt_toolkit.layout import HSplit, Layout, VSplit
10from prompt_toolkit.styles import Style
11from prompt_toolkit.widgets import Box, Button, Frame, Label, TextArea
12
13
14# Event handlers for all the buttons.
15def button1_clicked():
16    text_area.text = "Button 1 clicked"
17
18
19def button2_clicked():
20    text_area.text = "Button 2 clicked"
21
22
23def button3_clicked():
24    text_area.text = "Button 3 clicked"
25
26
27def exit_clicked():
28    get_app().exit()
29
30
31# All the widgets for the UI.
32button1 = Button("Button 1", handler=button1_clicked)
33button2 = Button("Button 2", handler=button2_clicked)
34button3 = Button("Button 3", handler=button3_clicked)
35button4 = Button("Exit", handler=exit_clicked)
36text_area = TextArea(focusable=True)
37
38
39# Combine all the widgets in a UI.
40# The `Box` object ensures that padding will be inserted around the containing
41# widget. It adapts automatically, unless an explicit `padding` amount is given.
42root_container = Box(
43    HSplit(
44        [
45            Label(text="Press `Tab` to move the focus."),
46            VSplit(
47                [
48                    Box(
49                        body=HSplit([button1, button2, button3, button4], padding=1),
50                        padding=1,
51                        style="class:left-pane",
52                    ),
53                    Box(body=Frame(text_area), padding=1, style="class:right-pane"),
54                ]
55            ),
56        ]
57    ),
58)
59
60layout = Layout(container=root_container, focused_element=button1)
61
62
63# Key bindings.
64kb = KeyBindings()
65kb.add("tab")(focus_next)
66kb.add("s-tab")(focus_previous)
67
68
69# Styling.
70style = Style(
71    [
72        ("left-pane", "bg:#888800 #000000"),
73        ("right-pane", "bg:#00aa00 #000000"),
74        ("button", "#000000"),
75        ("button-arrow", "#000000"),
76        ("button focused", "bg:#ff0000"),
77        ("text-area focused", "bg:#ff0000"),
78    ]
79)
80
81
82# Build a main application object.
83application = Application(layout=layout, key_bindings=kb, style=style, full_screen=True)
84
85
86def main():
87    application.run()
88
89
90if __name__ == "__main__":
91    main()
92