GUI Elements¶

gui_elements.py¶
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 | """
Show how to use GUI elements.
You can run this example with:
python -m arcade.examples.gui_elements_example
To style, see the style example or use a yaml file.
See:
https://github.com/pvcraven/arcade/blob/development/arcade/resources/style/default.yml
and the UIStyle.from_file() command.
"""
import arcade
import arcade.gui
from arcade.gui import UIManager
class MyFlatButton(arcade.gui.UIFlatButton):
"""
To capture a button click, subclass the button and override on_click.
"""
def on_click(self):
""" Called when user lets off button """
print("Click flat button. ")
class MyGhostFlatButton(arcade.gui.UIGhostFlatButton):
"""
For this subclass, we create a custom init, that takes in another
parameter, the UI text box. We use that parameter and print the contents
of the text entry box when the ghost button is clicked.
"""
def __init__(self, center_x, center_y, input_box):
super().__init__(
'GhostFlatButton',
center_x=center_x,
center_y=center_y,
width=250,
# height=20
)
self.input_box = input_box
def on_click(self):
""" Called when user lets off button """
print(f"Click ghost flat button. {self.input_box.text}")
class MyView(arcade.View):
"""
Main view. Really the only view in this example. """
def __init__(self):
super().__init__()
self.ui_manager = UIManager()
def on_draw(self):
""" Draw this view. GUI elements are automatically drawn. """
arcade.start_render()
def on_show_view(self):
""" Called once when view is activated. """
self.setup()
arcade.set_background_color(arcade.color.BLACK)
def on_hide_view(self):
self.ui_manager.unregister_handlers()
def setup(self):
""" Set up this view. """
self.ui_manager.purge_ui_elements()
y_slot = self.window.height // 4
left_column_x = self.window.width // 4
right_column_x = 3 * self.window.width // 4
# left side elements
self.ui_manager.add_ui_element(arcade.gui.UILabel(
'UILabel',
center_x=left_column_x,
center_y=y_slot * 3,
))
ui_input_box = arcade.gui.UIInputBox(
center_x=left_column_x,
center_y=y_slot * 2,
width=300
)
ui_input_box.text = 'UIInputBox'
ui_input_box.cursor_index = len(ui_input_box.text)
self.ui_manager.add_ui_element(ui_input_box)
button_normal = arcade.load_texture(':resources:gui_basic_assets/red_button_normal.png')
hovered_texture = arcade.load_texture(':resources:gui_basic_assets/red_button_hover.png')
pressed_texture = arcade.load_texture(':resources:gui_basic_assets/red_button_press.png')
button = arcade.gui.UIImageButton(
center_x=left_column_x,
center_y=y_slot * 1,
normal_texture=button_normal,
hover_texture=hovered_texture,
press_texture=pressed_texture,
text='UIImageButton'
)
self.ui_manager.add_ui_element(button)
# right side elements
button = MyFlatButton(
'FlatButton',
center_x=right_column_x,
center_y=y_slot * 1,
width=250,
# height=20
)
self.ui_manager.add_ui_element(button)
button = MyGhostFlatButton(
center_x=right_column_x,
center_y=y_slot * 2,
input_box=ui_input_box
)
self.ui_manager.add_ui_element(button)
if __name__ == '__main__':
window = arcade.Window(title='ARCADE_GUI')
view = MyView()
window.show_view(view)
arcade.run()
|