Flat Text Button Styled

gui_flat_button_styled.py
 1"""
 2Example code showing how to style UIFlatButtons.
 3
 4If Python and Arcade are installed, this example can be run from the command line with:
 5python -m arcade.examples.gui_flat_button_styled
 6"""
 7import arcade
 8import arcade.gui
 9import arcade.gui.widgets.buttons
10import arcade.gui.widgets.layout
11from arcade.gui import UIFlatButton
12
13
14class MyView(arcade.View):
15    def __init__(self):
16        super().__init__()
17        # --- Required for all code that uses UI element,
18        # a UIManager to handle the UI.
19        self.ui = arcade.gui.UIManager()
20
21        # Render button
22        red_style = {
23            "normal": UIFlatButton.UIStyle(
24                font_size=12,
25                font_name=("calibri", "arial"),
26                font_color=arcade.color.WHITE,
27                bg=arcade.color.RED,
28                border=None,
29                border_width=0,
30            ),
31            "hover": UIFlatButton.UIStyle(
32                font_size=12,
33                font_name=("calibri", "arial"),
34                font_color=arcade.color.WHITE,
35                bg=arcade.color.REDWOOD,
36                border=arcade.color.RED,
37                border_width=2,
38            ),
39            "press": UIFlatButton.UIStyle(
40                font_size=12,
41                font_name=("calibri", "arial"),
42                font_color=arcade.color.WHITE,
43                bg=arcade.color.RED_BROWN,
44                border=arcade.color.REDWOOD,
45                border_width=2,
46            ),
47            "disabled": UIFlatButton.UIStyle(
48                font_size=12,
49                font_name=("calibri", "arial"),
50                font_color=arcade.color.WHITE,
51                bg=arcade.color.COOL_GREY,
52                border=arcade.color.ASH_GREY,
53                border_width=2,
54            )
55        }
56
57        # Create a vertical BoxGroup to align buttons
58        self.v_box = arcade.gui.widgets.layout.UIBoxLayout(space_between=20)
59
60        # Create the buttons
61        demo_button_1 = arcade.gui.widgets.buttons.UIFlatButton(
62            text="Demo 1", width=200, style=UIFlatButton.DEFAULT_STYLE
63        )
64        demo_button_2 = arcade.gui.widgets.buttons.UIFlatButton(
65            text="Demo 2", width=200, style=red_style
66        )
67
68        self.v_box.add(demo_button_1)
69        self.v_box.add(demo_button_2)
70
71        # Create a widget to hold the v_box widget, that will center the buttons
72        ui_anchor_layout = arcade.gui.widgets.layout.UIAnchorLayout()
73        ui_anchor_layout.add(child=self.v_box, anchor_x="center_x", anchor_y="center_y")
74
75        self.ui.add(ui_anchor_layout)
76
77    def on_show_view(self):
78        self.window.background_color = arcade.color.DARK_BLUE_GRAY
79        # Enable UIManager when view is shown to catch window events
80        self.ui.enable()
81
82    def on_hide_view(self):
83        # Disable UIManager when view gets inactive
84        self.ui.disable()
85
86    def on_draw(self):
87        self.clear()
88        self.ui.draw()
89
90
91if __name__ == '__main__':
92    window = arcade.Window(800, 600, "UIExample", resizable=True)
93    window.show_view(MyView())
94    window.run()