pythonpycharmarcade

Python Arcade Styled Button


Programm says when running :

 font_name = style.get("font_name", UIFlatButton.UIStyle.font_name)
                ^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'get'

This shouldn't occur when using arcade I tried different code from the internet and the styling worked. But it doesn't work for me. Here is my code because I think it has to do with my code structure? please if anybody finds a reason it doesn't work tell me, I tried so much stuff and just don't understand the problem

#Module importieren
import arcade
import arcade.gui
import arcade.gui.widgets.buttons
import arcade.gui.widgets.layout
from arcade.gui import UIFlatButton

#Variablen definieren
width = 1200
height = 800
title = "Black Jack Arcade"
button_words = ["Hit", "Stand", "Split", "Double Down"]

border_color = arcade.color.ZINNWALDITE_BROWN

# Constants for sizing
CARD_SCALE = 0.6

# How big are the cards?
CARD_WIDTH = 140 * CARD_SCALE
CARD_HEIGHT = 190 * CARD_SCALE

# How big is the mat we'll place the card on?
MAT_PERCENT_OVERSIZE = 1.25
MAT_HEIGHT = int(CARD_HEIGHT * MAT_PERCENT_OVERSIZE)
MAT_WIDTH = int(CARD_WIDTH * MAT_PERCENT_OVERSIZE)

# How much space do we leave as a gap between the mats?
# Done as a percent of the mat size.
VERTICAL_MARGIN_PERCENT = 0.10
HORIZONTAL_MARGIN_PERCENT = 0.10

# The Y of the bottom row (2 piles)
BOTTOM_Y = MAT_HEIGHT / 2 + MAT_HEIGHT * VERTICAL_MARGIN_PERCENT

# The X of where to start putting things on the left side
START_X = MAT_WIDTH / 2 + MAT_WIDTH * HORIZONTAL_MARGIN_PERCENT

# Card constants
CARD_VALUES = ["A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"]
CARD_SUITS = ["Kreuz", "Herz", "Pik", "Karo"]

#klassen erstellen

class graphic(arcade.Window):
    def __init__(self):
        super().__init__(width, height, title)
        #Initialisierung anderer Module
        self.ui = arcade.gui.UIManager()
        self.ui.enable()

        #startet die Buttons
        self.create_click()

        arcade.set_background_color(arcade.color.AO)



    # Hier werden alle externen Texturen geladen und eingesetzt
    def load_textures(self):
        # create wooden background
        wood_back = arcade.load_texture("/Users/maximilianstriegler/Desktop/Golden Black Jack/game_textures/background_wood.jpg")
        arcade.draw_lbwh_rectangle_textured(0, 0, 1200, 800, wood_back)

        # create green felt texture
        playing_field = arcade.load_texture("/Users/maximilianstriegler/Desktop/Golden Black Jack/game_textures/background_felt.jpg")
        arcade.draw_lbwh_rectangle_textured(250, 50, 900, 700, playing_field)
        arcade.draw_rectangle_outline(700, 400, 900, 700, border_color, 3)


    # Funktion um Knöpfe zu erstellen
    def create_button(self, x, y, width, height, color, border_width):
        arcade.draw_xywh_rectangle_filled(x - width / 2, y - height / 2, width, height, color)
        arcade.draw_rectangle_outline(x, y, width, height, border_color, border_width)


    def create_click(self):
        button_style = {
            "big" : UIFlatButton.UIStyle(
               font_size=20,
                font_name=('calibri', 'arial'),
                font_color=arcade.color.RED,
                bg=arcade.color.BLUE,
                border=arcade.color.BLUE,
                border_width=2,
            )
        }
        #create buttons
        for i in range(len(button_words)):
            buttons = arcade.gui.UIFlatButton(text=button_words[i], x=30, y=630 - (i * 150), width=190, height=100,
                                              style=button_style)

            buttons.on_click = self.on_click_button

            self.ui.add(buttons)


        exit_button = arcade.gui.UIFlatButton(text= "Exit", x= 77.5, y= 70, width= 95, height=50)
        exit_button.on_click = self.on_exit_quit

        self.ui.add(exit_button)


    #Funktion große Buttons
    def on_click_button(self, event):

            print("JAAAAA")


    #Funktion Exit Button
    def on_exit_quit(self, event):
        print("Exit Button Clicked")


    def on_draw(self):

        #Rendern starten
        arcade.start_render()

        #Texturen laden
        self.load_textures()

        #Buttons laden
        self.ui.draw()


        #Rendern beenden
        arcade.finish_render()


def start():
    window = graphic()
    arcade.run()

if __name__ == "__main__":
    start()

I was expecting the button to be styled but it wasn't. Not even my prof could help me with this problem and this dudes programming AI and stuff like that.


Solution

  • You're incorrectly defined button_style. Instead of big style you need to define normal, hover and press styles. Look at the fixed code:

    import arcade
    import arcade.gui
    import arcade.gui.widgets.buttons
    import arcade.gui.widgets.layout
    from arcade.gui import UIFlatButton
    
    BUTTON_WORDS = ['Hit', 'Stand', 'Split', 'Double Down']
    
    
    class Graphic(arcade.Window):
        def __init__(self):
            super().__init__(title='Black Jack Arcade')
            self.ui = arcade.gui.UIManager()
            self.v_box = arcade.gui.widgets.layout.UIBoxLayout(space_between=20)
            ui_anchor_layout = arcade.gui.widgets.layout.UIAnchorLayout()
            ui_anchor_layout.add(child=self.v_box, anchor_x='center_x', anchor_y='center_y')
            button_style = {
                'normal': UIFlatButton.UIStyle(
                    font_size=12,
                    font_name=('calibri', 'arial'),
                    font_color=arcade.color.WHITE,
                    bg=arcade.color.RED,
                    border=None,
                    border_width=0,
                ),
                'hover': UIFlatButton.UIStyle(
                    font_size=12,
                    font_name=('calibri', 'arial'),
                    font_color=arcade.color.WHITE,
                    bg=arcade.color.REDWOOD,
                    border=arcade.color.RED,
                    border_width=2,
                ),
                'press': UIFlatButton.UIStyle(
                    font_size=12,
                    font_name=('calibri', 'arial'),
                    font_color=arcade.color.WHITE,
                    bg=arcade.color.RED_BROWN,
                    border=arcade.color.REDWOOD,
                    border_width=2,
                )
            }
            for i in range(len(BUTTON_WORDS)):
                buttons = arcade.gui.UIFlatButton(text=BUTTON_WORDS[i], x=30, y=630 - (i * 150), width=190, height=100, style=button_style)
                buttons.on_click = self.on_click_button
                self.v_box.add(buttons)
            exit_button = arcade.gui.UIFlatButton(text='Exit', x=77.5, y=70, width=95, height=50)
            exit_button.on_click = self.on_exit_quit
            self.ui.add(exit_button)
            self.ui.add(ui_anchor_layout)
            self.ui.enable()
            arcade.set_background_color(arcade.color.AO)
    
        def on_click_button(self, event):
            print('JAAAAA')
    
        def on_exit_quit(self, event):
            print('Exit Button Clicked')
    
        def on_draw(self):
            self.clear()
            self.ui.draw()
    
    
    Graphic()
    arcade.run()
    

    enter image description here