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

从主菜单类调用后Tkinter网格文本不显示问题求助

Wumpus世界游戏网格文本不显示问题修复

问题情况

单独运行aigui()函数时,solve_wumpus_world方法可正常在Tkinter网格中显示文本;但通过maingui()函数调用aigui()后,游戏逻辑正常、网格能高亮,唯独网格内的文本无法显示。

问题原因

Tkinter应用程序应当仅存在一个主Tk()实例,当maingui()已经创建了一个Tk()实例后,aigui()中再次创建新的Tk()实例会导致界面渲染冲突,从而出现文本不显示的异常。

解决方案

将aigui()中的新Tk()实例替换为Toplevel()(子窗口),并移除aigui()中的独立主循环,复用maingui()的主循环。

修改后的代码

def solve_wumpus_world(master, world_file):
    world = World()
    world.generate_world(world_file)

    label_grid = [[Grid_Label(master, i, j) for j in range(world.num_cols)] for i in range(world.num_rows)]
    agent = Agent(world, label_grid)

    # Agent Solving
    while agent.exited == False:
        agent.explore()
        if agent.found_gold == True:
            agent.leave_cave()
        break

    agent.repaint_world()
    agent.world_knowledge[agent.world.agent_row][agent.world.agent_col].remove('A')
    time.sleep(1.5)
    agent.repaint_world()

# GUI
def aigui(root):
    # 使用Toplevel创建子窗口,而非新的Tk实例
    game = Toplevel(root)
    game.title("Hunt the Wumpus")
    game.configure(bg="DarkOrchid4")
    game.geometry("2000x2000")
    game.iconbitmap(r'Images\Monster.ico')
    world = World()
    world.generate_world("world_1.txt")

    grid = LabelFrame(game, text="GRID", fg="lawn green", bg="DarkOrchid4", padx=10, pady=10, font="Creepster 24")
    worldLabel = LabelFrame(game, text="MENU", fg="lawn green", bg="DarkOrchid4", padx=10, pady=10, font="Creepster 20")

    label_grid = [[Grid_Label(grid, i, j) for j in range(world.num_cols)] for i in range(world.num_rows)]

    world_1 = Button(worldLabel, text="WORLD ONE", command=lambda: solve_wumpus_world(grid, "world_1.txt"), padx=40,
                            font="Leichenhaus 16 bold", bg="DarkViolet", fg="lawn green", borderwidth=0,
                            activeforeground="chartreuse2", activebackground="Indigo")
    world_2 = Button(worldLabel, text="WORLD TWO", command=lambda: solve_wumpus_world(grid, "world_2.txt"), padx=40,
                            font="Leichenhaus 16 bold", bg="DarkViolet", fg="lawn green", borderwidth=0,
                            activeforeground="chartreuse2", activebackground="Indigo")
    world_3 = Button(worldLabel, text="WORLD THREE", command=lambda: solve_wumpus_world(grid, "world_3.txt"), padx=40,
                            font="Leichenhaus 16 bold", bg="DarkViolet", fg="lawn green", borderwidth=0,
                            activeforeground="chartreuse2", activebackground="Indigo")
    world_4 = Button(worldLabel, text="WORLD FOUR", command=lambda: solve_wumpus_world(grid, "world_4.txt"), padx=40,
                            font="Leichenhaus 16 bold", bg="DarkViolet", fg="lawn green", borderwidth=0,
                            activeforeground="chartreuse2", activebackground="Indigo")
    quitButton = Button(worldLabel, text="QUIT", padx=40, font="Leichenhaus 16 bold", bg="DarkViolet", fg="lawn green",
                                borderwidth=0, activeforeground="chartreuse2", activebackground="Indigo", command=lambda:game.destroy())

    grid.pack(padx=1,pady=1)
    worldLabel.pack(padx=5, pady=5)
    world_1.grid(row=0, column=0, padx=10, pady=10, sticky=EW)
    world_2.grid(row=0, column=1, padx=10, pady=10, sticky=EW)
    world_3.grid(row=0, column=2, padx=10, pady=10, sticky=EW)
    world_4.grid(row=0, column=3, padx=10, pady=10, sticky=EW)
    quitButton.grid(row=1, column=1, columnspan=2, padx=10, pady=10, sticky=EW)

    # 移除独立的mainloop,复用主窗口的循环
    # game.mainloop()


def maingui():
    root = Tk()
    root.geometry("600x600")
    root.title("Hunt the Wumpus - Game")
    root.configure(bg="DarkOrchid4")
    root.iconbitmap(r'Images\Monster.ico')

    # Label
    myLabel = LabelFrame(root, text="Hunt the Wumpus", fg="lawn green", bg="DarkOrchid4", padx=100, pady=100)
    textFont = ("Creepster", 24)
    myLabel.config(font=textFont)

    # Buttons
    buttonFont = ("Leichenhaus", 16)
    # 传递root给aigui函数
    aiButton = Button(myLabel, text="GAME", fg="lawn green", bg="DarkViolet",
                    activeforeground="chartreuse2", activebackground="Indigo", padx=40, command=lambda: aigui(root))
    aiButton.config(font=buttonFont)
    creditButton = Button(myLabel, text="Credits", fg="lawn green", bg="DarkViolet",
                        activeforeground="chartreuse2", activebackground="Indigo", padx=40)
    creditButton.config(font=buttonFont)
    quitButton = Button(myLabel, text="Quit", fg="lawn green", bg="DarkViolet",
                        activeforeground="chartreuse2", activebackground="Indigo", padx=40, command=lambda: root.destroy())
    quitButton.config(font=buttonFont)

    # Packing on Screen
    myLabel.pack(padx=10, pady=10)
    aiButton.grid(row=0, column=2, padx=10, pady=10, sticky=EW)
    creditButton.grid(row=1, column=2, padx=10, pady=10, sticky=EW)
    quitButton.grid(row=3, column=2, padx=10, pady=10, sticky=EW)

    root.mainloop()

maingui()

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.06 00:10:50