GUI Scroll Area

The following example demonstrates how to make use of the experimental UIScrollLayout widget.

Screen shot
exp_scroll_area.py
 1"""This example is a proof-of-concept for a scrollable area.
 2
 3The example shows vertical and horizontal scroll areas with a list of buttons.
 4
 5The current implementation lags a proper API, customizability, mouse support and documentation,
 6but shows how to use the current experimental feature.
 7
 8If Arcade and Python are properly installed, you can run this example with:
 9python -m arcade.examples.gui.exp_scroll_area
10"""
11
12import arcade
13from arcade.gui import UIAnchorLayout, UIBoxLayout, UIFlatButton, UIView
14from arcade.gui.experimental import UIScrollArea
15from arcade.gui.experimental.scroll_area import UIScrollBar
16
17
18class MyView(UIView):
19    def __init__(self):
20        super().__init__()
21        self.background_color = arcade.uicolor.BLUE_BELIZE_HOLE
22
23        # create a layout with two columns
24        root = self.add_widget(UIAnchorLayout())
25        content_left = UIAnchorLayout(size_hint=(0.5, 1))
26        root.add(content_left, anchor_x="left", anchor_y="center")
27        content_right = UIAnchorLayout(size_hint=(0.5, 1))
28        root.add(content_right, anchor_x="right", anchor_y="center")
29
30        # create the list, which should be scrolled vertically
31        vertical_list = UIBoxLayout(size_hint=(1, 0), space_between=1)
32        for i in range(100):
33            button = UIFlatButton(height=30, size_hint=(1, None), text=f"Button {i}")
34            vertical_list.add(button)
35
36        # the scroll area and the scrollbar are added to a box layout
37        # so they are next to each other, this also reduces complexity for the layout
38        # implementation
39        v_scroll_area = UIBoxLayout(vertical=False, size_hint=(0.8, 0.8))
40        content_left.add(v_scroll_area, anchor_x="center", anchor_y="center")
41
42        scroll_layout = v_scroll_area.add(UIScrollArea(size_hint=(1, 1)))
43        scroll_layout.with_border(color=arcade.uicolor.WHITE_CLOUDS)
44        scroll_layout.add(vertical_list)
45
46        v_scroll_area.add(UIScrollBar(scroll_layout))
47
48        # create the list, which should be scrolled vertically
49        horizontal_list = UIBoxLayout(size_hint=(0, 1), space_between=1, vertical=False)
50        for i in range(100):
51            button = UIFlatButton(width=50, size_hint=(None, 1), text=f"B{i}")
52            horizontal_list.add(button)
53
54        # same as above, but for horizontal scrolling
55        h_scroll_area = UIBoxLayout(vertical=True, size_hint=(0.8, 0.8))
56        content_right.add(h_scroll_area, anchor_x="center", anchor_y="center")
57
58        h_scroll_layout = h_scroll_area.add(UIScrollArea(size_hint=(1, 1)))
59        h_scroll_layout.with_border(color=arcade.uicolor.WHITE_CLOUDS)
60        h_scroll_layout.add(horizontal_list)
61
62        h_scroll_area.add(UIScrollBar(h_scroll_layout, vertical=False))
63
64    def on_key_press(self, symbol: int, modifiers: int) -> bool | None:
65        if symbol == arcade.key.ESCAPE:
66            arcade.close_window()
67            return True
68
69        return False
70
71
72def main():
73    window = arcade.Window(title="GUI Example: UIScrollLayout")
74    window.show_view(MyView())
75    window.run()
76
77
78if __name__ == "__main__":
79    main()