From e106bf94eff07d9a59771d9ccc4406421e18ab64 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sat, 4 May 2024 19:35:20 +0200 Subject: Adding upstream version 3.0.36. Signed-off-by: Daniel Baumann --- examples/full-screen/buttons.py | 91 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 91 insertions(+) create mode 100755 examples/full-screen/buttons.py (limited to 'examples/full-screen/buttons.py') diff --git a/examples/full-screen/buttons.py b/examples/full-screen/buttons.py new file mode 100755 index 0000000..540194d --- /dev/null +++ b/examples/full-screen/buttons.py @@ -0,0 +1,91 @@ +#!/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() -- cgit v1.2.3