Python Arcade – Adding Buttons
In this article, we will learn How we can create buttons in Arcade using python.
Adding Buttons
In Arcade we can easily add buttons to our game.
For this, we will use some functions:
UIManager():
Syntax: arcade.gui.UIManager(window, auto_enable)
Parameters:
- window : Our game window
- auto_enable : Accepts a boolean value
UIBoxLayout():
Syntax: arcade.gui.UIBoxLayout(x, y, vertical, align, children, size)
Parameters:
- x : x coordinate of bottom left
- y : x coordinate of bottom left
- vertical : Layout children vertical (True) or horizontal (False)
- align : Align children in orthogonal direction (x: left, center, right / y: top, center, bottom)
- children : Initial children, more can be added
- size : A hint for UILayout, if this UIWidget would like to grow
UIFlatButton():
Syntax: arcade.gui.UIFlatButton( x, y, width, height, text, style)
Parameters:
- x : x-coordinate of widget.
- y : y-coordinate of widget.
- width : width of widget. Defaults to texture width if not specified.
- height : height of widget. Defaults to texture height if not specified.
- text : text to add to the button.
- style : Used to style the button
Now to create our button we are going to create a class named MainClass and inside this class, we are going to initialize one variable for the UIManager. After that, we will create our button using the UIFlatButton() then we will add this button in our UIManager. Then we will create an on_draw() function to draw our button.
Below is the implementation:
Python3
import arcade
import arcade.gui
class MainGame(arcade.Window):
def __init__( self ):
super ().__init__( 600 , 600 , title = "Buttons" )
arcade.set_background_color(arcade.color.BLUE)
self .uimanager = arcade.gui.UIManager()
self .uimanager.enable()
start_button = arcade.gui.UIFlatButton(text = "Start Game" ,
width = 200 )
self .uimanager.add(
arcade.gui.UIAnchorWidget(
anchor_x = "center_x" ,
anchor_y = "center_y" ,
child = start_button)
)
def on_draw( self ):
arcade.start_render()
self .uimanager.draw()
MainGame()
arcade.run()
|
Output:
Adding functions in Button
Now we are going to create an on_buttonclick() function which will be called every time the user presses the button.
Python3
import arcade
import arcade.gui
class MainGame(arcade.Window):
def __init__( self ):
super ().__init__( 600 , 600 , title = "Buttons" )
arcade.set_background_color(arcade.color.BLUE)
self .uimanager = arcade.gui.UIManager()
self .uimanager.enable()
start_button = arcade.gui.UIFlatButton(text = "Start Game" ,
width = 200 )
start_button.on_click = self .on_buttonclick
self .uimanager.add(
arcade.gui.UIAnchorWidget(
anchor_x = "center_x" ,
anchor_y = "center_y" ,
child = start_button)
)
def on_buttonclick( self , event):
print ( "Button is clicked" )
def on_draw( self ):
arcade.start_render()
self .uimanager.draw()
MainGame()
arcade.run()
|
Output:
Last Updated :
23 Sep, 2021
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...