Python Arcade 风格按钮

问题描述 投票:0回答:1

程序运行时显示:

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

使用街机时不应该发生这种情况,我尝试了来自互联网的不同代码并且样式有效。但这对我不起作用。这是我的代码,因为我认为这与我的代码结构有关?如果有人找到它不起作用的原因,请告诉我,我尝试了很多东西,但就是不明白问题

#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()

我原本希望按钮有样式,但事实并非如此。甚至我的教授也无法帮助我解决这个问题,而这个家伙正在编程人工智能之类的东西。

python pycharm arcade
1个回答
0
投票

您的定义不正确

button_style
。您需要定义
big
normal
hover
样式,而不是
press
样式。看固定代码:

import arcade
import arcade.gui
import arcade.gui.widgets.buttons
import arcade.gui.widgets.layout
from arcade.gui import UIFlatButton

BORDER_COLOR = arcade.color.ZINNWALDITE_BROWN
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)
        self.wood_back = arcade.load_texture('background_wood.jpg')
        self.playing_field = arcade.load_texture('background_felt.jpg')
        arcade.draw_rectangle_outline(700, 400, 900, 700, BORDER_COLOR, 3)

    def on_click_button(self, event):
        print('JAAAAA')

    def on_exit_quit(self, event):
        print('Exit Button Clicked')

    def on_draw(self):
        self.clear()
        arcade.draw_lbwh_rectangle_textured(0, 0, 1200, 800, self.wood_back)
        arcade.draw_lbwh_rectangle_textured(250, 50, 900, 700, self.playing_field)
        self.ui.draw()


Graphic()
arcade.run()

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.