You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

PySimpleGUI动态更新按钮问题:文本冒险游戏循环实现求助

问题描述

我正在开发一款基于文本的循环式「选择你的冒险」故事游戏,代码拆分为多个函数。每个函数会返回三个内容:

  • 故事文本(字符串格式)
  • 玩家选项(字典,用于更新按钮文本)
  • 对应选择的下一场景函数(字典)

预期效果:点击屏幕下方的两个选项按钮后,能即时更新屏幕上的故事文本和按钮内容。

目前遇到的问题:不知道如何在窗口创建后构建循环逻辑,按钮点击后无法触发场景切换,怀疑按钮映射存在错误,需要解决方案。

当前main函数代码:

import PySimpleGUI as sg
def main ():
        sg.theme("default1")   
        # All the stuff inside your window.
        char_info = character_creation()
        char_name = char_info[0]
        char_gender = char_info[1]
    
        scene_loading(char_name,char_gender)
    
        # Maps the player choices to the variable current_scene
        current_scene = cabin_scene(char_name, char_gender)
        # Returns (story_text, player_choices, next_scenes)
    
        # Pulls display text from current scene to display to user
        display_text = current_scene [0]
        
        # Pulls returned options from current scene to be mapped to display
        current_options = current_scene [1]
        option_1 = current_options ["option_1"]
        option_2 = current_options ["option_2"]
    
        # Format of dictionary values
        # {"option_1":first_scene,"option_2":second_scene}
        next_scenes = current_scene [2]
        first_scene = next_scenes ["option_1"]
        second_scene = next_scenes ["option_2"]
    
        layout = [  [sg.Text(display_text) ],
            [sg.Button(option_1),sg.Button(option_2)], 
            [sg.Button("Cancel")] ]
     
        # Create the Window
        window = sg.Window("The Ultimate Choose Your Own Adventure Story", layout)
        # Event Loop to process "events" and get the "values" of the inputs
        while True:
            event, values = window.read()
            if event == sg.WIN_CLOSED or event == "Cancel": # if user closes window or clicks cancel
                break
     
        window. Close()

场景函数返回值示例:

story_text = f"""Insert text personalized to user here."""
player_choices = {
        "option_1":"HIDE",
        "option_2":"ESCAPE"}

next_scenes = {
        "option_1":closet_scene,
        "option_2":forest_scene}

return story_text, player_choices, next_scenes
解决方案

核心问题在于当前代码没有在事件循环中处理按钮点击后的场景更新逻辑,也没有保留场景切换的关键数据。以下是修改后的完整main函数:

import PySimpleGUI as sg

def main():
    sg.theme("default1")   
    # 初始化角色信息
    char_info = character_creation()
    char_name, char_gender = char_info
    
    scene_loading(char_name, char_gender)
    
    # 初始化当前场景函数引用
    current_scene_func = cabin_scene
    # 获取初始场景内容
    story_text, player_choices, next_scenes = current_scene_func(char_name, char_gender)
    
    # 定义布局,给文本和按钮添加key以便后续更新
    layout = [
        [sg.Text(story_text, key="-STORY-")],
        [sg.Button(player_choices["option_1"], key="-OPT1-"), sg.Button(player_choices["option_2"], key="-OPT2-")],
        [sg.Button("Cancel")]
    ]
    
    window = sg.Window("The Ultimate Choose Your Own Adventure Story", layout)
    
    # 事件循环处理逻辑
    while True:
        event, values = window.read()
        
        if event == sg.WIN_CLOSED or event == "Cancel":
            break
        
        # 判断点击的选项按钮,切换对应场景函数
        if event == window["-OPT1-"].GetText():
            current_scene_func = next_scenes["option_1"]
        elif event == window["-OPT2-"].GetText():
            current_scene_func = next_scenes["option_2"]
        
        # 获取新场景内容
        story_text, player_choices, next_scenes = current_scene_func(char_name, char_gender)
        
        # 更新UI内容
        window["-STORY-"].update(story_text)
        window["-OPT1-"].update(player_choices["option_1"])
        window["-OPT2-"].update(player_choices["option_2"])
    
    window.close()

修改说明

  • 保留场景函数引用:用current_scene_func变量存储当前场景的函数,方便后续切换调用。
  • 添加组件Key:给文本和按钮设置key属性,这样可以在事件循环中精准定位组件并更新内容。
  • 事件循环内处理切换:在循环中判断用户点击的按钮文本,匹配对应的下一场景函数,调用后实时更新UI。
  • 简化变量赋值:用解包方式处理角色信息,让代码更简洁易读。

修改后,点击选项按钮即可触发场景切换,实时更新故事文本和按钮选项,实现循环式的冒险流程。

内容的提问来源于stack exchange,提问作者Joseph Kemper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 08:05:26