Flat Text Buttons#
For an introduction the GUI system, see GUI Concepts.
The arcade.gui.UIFlatButton
is a simple button with a text label.
It doesn’t have any three-dimensional look to it.
There are three ways to process button click events:
Create a class with a parent class of arcade.UIFlatButton and implement a method called on_click.
Create a button, then set the on_click attribute of that button to equal the function you want to be called.
Create a button. Then use a decorator to specify a method to call when an on_click event occurs for that button.
This code shows each of the three ways above. Code should pick ONE of the three ways and standardize on it though-out the program. Do NOT write code that uses all three ways.
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 | """ Example code showing how to create a button, and the three ways to process button events. If Python and Arcade are installed, this example can be run from the command line with: python -m arcade.examples.gui_flat_button """ import arcade import arcade.gui # --- Method 1 for handling click events, # Create a child class. import arcade.gui.widgets.buttons import arcade.gui.widgets.layout class QuitButton(arcade.gui.widgets.buttons.UIFlatButton): def on_click(self, event: arcade.gui.UIOnClickEvent): arcade.exit() class MyWindow(arcade.Window): def __init__(self): super().__init__(800, 600, "UIFlatButton Example", resizable=True) # --- Required for all code that uses UI element, # a UIManager to handle the UI. self.manager = arcade.gui.UIManager() self.manager.enable() # Set background color arcade.set_background_color(arcade.color.DARK_BLUE_GRAY) # Create a vertical BoxGroup to align buttons self.v_box = arcade.gui.widgets.layout.UIBoxLayout(space_between=20) # Create the buttons start_button = arcade.gui.widgets.buttons.UIFlatButton( text="Start Game", width=200 ) self.v_box.add(start_button) settings_button = arcade.gui.widgets.buttons.UIFlatButton( text="Settings", width=200 ) self.v_box.add(settings_button) # Again, method 1. Use a child class to handle events. quit_button = QuitButton(text="Quit", width=200) self.v_box.add(quit_button) # --- Method 2 for handling click events, # assign self.on_click_start as callback start_button.on_click = self.on_click_start # --- Method 3 for handling click events, # use a decorator to handle on_click events @settings_button.event("on_click") def on_click_settings(event): print("Settings:", event) # Create a widget to hold the v_box widget, that will center the buttons ui_anchor_layout = arcade.gui.widgets.layout.UIAnchorLayout() ui_anchor_layout.add(child=self.v_box, anchor_x="center_x", anchor_y="center_y") self.manager.add(ui_anchor_layout) def on_click_start(self, event): print("Start:", event) def on_draw(self): self.clear() self.manager.draw() window = MyWindow() arcade.run() |