为什么我在 python arcade 部分的按钮似乎不活动?

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

你好,这是我在 stackoverflow 上的第一篇文章,所以如果我的文章不相关或有问题,我提前道歉。

我正在使用 python 库 Arcade 和 Arcade.gui。

目前我尝试在 Section 实例中添加 GUI 小部件(在下面的代码中命名为 popup)。该按钮是可见的,但我的问题是:它似乎处于非活动状态。它对悬停或单击没有反应(图像与源代码位于同一文件夹中)

感谢您的帮助。

我的最小代码:

import arcade
import arcade.gui

class Popup(arcade.Section):

    def __init__(self, left: int, bottom: int, width: int, height: int, **kwargs):
        super().__init__(left, bottom, width, height, **kwargs)

        self.ui_manager = arcade.gui.UIManager()
        self.ui_manager.enable()

        box = arcade.gui.UIBoxLayout()

        button = arcade.gui.UITextureButton(
            x=0, y=0,
            texture=arcade.load_texture('button.png'),
            texture_hovered=arcade.load_texture('button_hovered.png'),
            texture_pressed=arcade.load_texture('button_pressed.png'),
            text='Click !'
        )

        box.add(button)
        button.on_click = self.on_click_button
        anchored_box = arcade.gui.UIAnchorWidget(anchor_x="center_x", anchor_y="center_y", child=box)

        self.manager.add(anchored_box)

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

    def on_draw(self):
        self.manager.draw()

class GameView(arcade.View):

    def __init__(self):
        super().__init__()

        popup_width = 400
        popup_height = 300
        popup_left = self.window.width // 2 - popup_width // 2
        popup_bottom = self.window.height // 2 - popup_height // 2

        self.popup = Popup(popup_left, popup_bottom, popup_width, popup_height, self.ui_manager)
        self.section_manager.add_section(self.popup)

    def on_draw(self):
        arcade.start_render()


def main():
    window = arcade.Window(width=800, height=600, title='title')
    game_view = GameView()
    window.show_view(game_view)
    window.run()


if __name__ == "__main__":
    main()

我还尝试在 gameView 中创建 GUImanager 并通过参数将其传递给该部分,它不会改变任何东西。

我查看了 Arcade 网站上的“Sections Demo 3”示例,但它没有使用 Arcade GUI 按钮。

python user-interface button arcade
© www.soinside.com 2019 - 2024. All rights reserved.