Python Arcade中UIFlatButton样式设置遇NoneType错误求助
你的问题出在样式字典的结构不符合Arcade UIFlatButton的规范,直接将所有样式属性放在根字典中会导致组件无法正确读取配置,进而触发font_name为None的错误。Arcade的UI组件样式需要按按钮状态(如normal、pressed)嵌套,文本相关样式还要放在对应状态的text_style子字典中。
修正后的代码
def create_click(self): # 按状态组织样式字典 button_style = { "normal": { "bg_color": arcade.color.BLUE, "border_color": arcade.color.BLACK, "border_width": 2, "text_style": { "font_name": ["Kenney Pixel", "Arial", "Kenney Bold"], "font_size": 20, "font_color": arcade.color.RED } }, "pressed": { "bg_color": arcade.color.LIGHT_BLUE, "border_color": arcade.color.BLACK, "text_style": { "font_color": arcade.color.WHITE } } } for i in range(0,4): start_button = arcade.gui.UIFlatButton( text="HIT", x=125, y=680 - (i * 150), width=190, height=100, style=button_style ) self.v_box.add(start_button) start_button.on_click = self.on_click_start # 无需重复添加到manager,v_box已属于manager的子元素,添加到v_box即可 # self.manager.add(start_button)
关键修改说明
- 状态化样式结构:将样式拆分为
normal(默认状态)和pressed(按下状态)两个子字典,分别对应不同状态下的按钮样式。 - 文本样式嵌套:所有文本相关配置(
font_name、font_size、font_color)必须放在对应状态的text_style子字典中,这是Arcade UI组件读取文本样式的标准路径。 - 移除重复添加:
v_box是UI管理器(manager)的子容器,将按钮添加到v_box后会自动被manager管理,无需再次调用self.manager.add(start_button)。
这样修改后,按钮的背景色、字体等样式就能正常生效,同时不会触发NoneType错误。
内容的提问来源于stack exchange,提问作者Maximilian Striegler
相关产品推荐
相关产品推荐

