pygame_gui 事件未触发

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

我正在尝试做一些我以前做过的事情,没有问题,那就是使用 pygame_gui 和 pygame 来检测何时单击了 UI 按钮。

import pygame as pg
import pygame_gui as pgg
import tkinter as tk
import tkinter.filedialog
import csv


character_dict = ()
last_character_gui = None


def main():
    pg.init()
    screen = pg.display.set_mode((800, 600))
    gui_manager = pgg.UIManager((800, 600))
    app_running = True
    clock = pg.time.Clock()
    open_file_button = pgg.elements.UIButton(relative_rect=pg.Rect(0, 0, 800, 50),
                                             manager=gui_manager,
                                             text="open file")
    scroll_container = pgg.elements.ui_scrolling_container.UIScrollingContainer(relative_rect=pg.Rect(0, 50, 800, 2000),
                                                                                manager=gui_manager)
    while app_running:
        time_delta = clock.tick(60) / 100.0
        gui_manager.update(time_delta)
        gui_manager.draw_ui(screen)
        pg.display.update()

        for event in pg.event.get():
            if event.type == pgg.UI_BUTTON_PRESSED:
                if event.ui_element == open_file_button:
                    file = prompt_file()
                    character_dict = populate_characters(file)
                    populate_gui(scroll_container, gui_manager)
            if event.type == pg.QUIT:
                app_running = False

问题是没有触发 UI_BUTTON_PRESSED 事件。我曾尝试跟踪事件输出,但它从未发生过。我曾尝试阅读文档,但可惜我看不出我在这里做错了什么,也看不出问题出在哪里。我将不胜感激任何帮助。

python pygame pygame-gui
© www.soinside.com 2019 - 2024. All rights reserved.