GUI Widgets#

Screen shot GUI Widgets
gui_widgets.py#
 1"""
 2Example code showing how to create some of the different UIWidgets.
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.gui_widgets
 6"""
 7from __future__ import annotations
 8
 9import arcade
10import arcade.gui
11import arcade.gui.widgets.buttons
12import arcade.gui.widgets.layout
13import arcade.gui.widgets.text
14
15
16class MyView(arcade.View):
17    def __init__(self):
18        super().__init__()
19        # --- Required for all code that uses UI element,
20        # a UIManager to handle the UI.
21        self.ui = arcade.gui.UIManager()
22
23        # Create a vertical BoxGroup to align buttons
24        self.v_box = arcade.gui.widgets.layout.UIBoxLayout(space_between=20)
25
26        # Create a text label
27        ui_text_label = arcade.gui.widgets.text.UITextArea(
28            text="This is a Text Widget",
29            width=450,
30            height=40,
31            font_size=24,
32            font_name="Kenney Future",
33        )
34        self.v_box.add(ui_text_label)
35
36        text = (
37            "The real danger is not that computers will begin to think like people, "
38            "but that people will begin "
39            "to think like computers. - Sydney Harris (Journalist)"
40        )
41        ui_text_label = arcade.gui.widgets.text.UITextArea(
42            text=text, width=450, height=60, font_size=12, font_name="Arial"
43        )
44        self.v_box.add(ui_text_label)
45
46        # Create a UIFlatButton
47        ui_flatbutton = arcade.gui.widgets.buttons.UIFlatButton(
48            text="Flat Button", width=200
49        )
50        self.v_box.add(ui_flatbutton)
51
52        # Handle Clicks
53        @ui_flatbutton.event("on_click")
54        def on_click_flatbutton(event):
55            print("UIFlatButton pressed", event)
56
57        # Create a UITextureButton
58        texture = arcade.load_texture(":resources:onscreen_controls/flat_dark/play.png")
59        ui_texture_button = arcade.gui.widgets.buttons.UITextureButton(texture=texture)
60
61        # Handle Clicks
62        @ui_texture_button.event("on_click")
63        def on_click_texture_button(event):
64            print("UITextureButton pressed", event)
65
66        self.v_box.add(ui_texture_button)
67
68        # Create a widget to hold the v_box widget, that will center the buttons
69        self.ui.add(
70            arcade.gui.widgets.layout.UIAnchorLayout(children=[self.v_box])
71        )
72
73    def on_click_start(self, event):
74        print("Start:", event)
75
76    def on_show_view(self):
77        self.window.background_color = arcade.color.DARK_BLUE_GRAY
78        # Enable UIManager when view is shown to catch window events
79        self.ui.enable()
80
81    def on_hide_view(self):
82        # Disable UIManager when view gets inactive
83        self.ui.disable()
84
85    def on_draw(self):
86        self.clear()
87        self.ui.draw()
88
89
90if __name__ == '__main__':
91    window = arcade.Window(800, 600, "UIExample", resizable=True)
92    window.show_view(MyView())
93    window.run()