Tkinter does not display text in the grid after calling the method from another class

39 views Asked by At

If I run the following code alone, it displays the text in the grid while running:

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():
    game = Tk()
    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)

    game.mainloop()

It shows the text inside the grid when gaming is being played

But when I call the above code from another class like a main class which has a main menu which further opens this game window, the game works fine and the boxes are also highlighted but the text isn't displayed. For example, the code below

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():
    game = Tk()
    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)

    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)

    # Functions




    # Buttons
    buttonFont = ("Leichenhaus", 16)
    aiButton = Button(myLabel, text="GAME", fg="lawn green", bg="DarkViolet",
                    activeforeground="chartreuse2", activebackground="Indigo", padx=40, command=aigui)
    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()

This code calls the above code from a method when the button is pressed.

Text not being displayed but the game is working fine.

0

There are 0 answers