Jack Frye
Jack Frye

Reputation: 583

Two Windows Opening When Only One Should

I am trying to make a game where a map Tk() window opens, the player chooses location, the map window closes, and the level window opens. When the player has a choice the leave the level and chooses 'yes', the level Tk() should close and the map should open back up so the player can click on a different location and open another Tk(). For some reason all Tks are opening at once. Here is my code.

class GUI_Control:
    def __init__(self, player, delegate, level=-1):
        self.delegate = delegate
        self.player = player
        self.current_level = level
        self.map = Map(self)
        self.current_level = level

        #define level gui's here and put in data structure
        hydra_level = Hydra_Level(self)
        self.windows = [hydra_level]

    def open(self):
        if self.current_level == -1:
            self.map.mainloop()
        else:
            self.current_level.mainloop()

    def save(self):
        self.delegate.save()

    def swap_window(self, n):
        #pull up the specified window
       self.windows[n].mainloop()



class Map(Tk):
    MAP_WIDTH = 600
    MAP_HEIGHT = 375

    def __init__(self, listener, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.listener = listener
        # define map gui here
        self.canvas = Canvas(self, width=self.MAP_WIDTH, height=self.MAP_HEIGHT)
        self.canvas.pack()
        self.map_picture = PhotoImage(file=r"images/archipelago.gif")
        self.canvas.create_image(0, 0, image=self.map_picture)

    def destroy(self, n=0):
        Tk.destroy(self)
        #send message back to gui_control to bring up another window
        self.listener.swap_window(n)

class Hydra_Level(Tk):
    def __init__(self, listener, *args, **kwargs):
        Tk.__init__(self, *args, **kwargs)
        self.listener = listener

    def destroy(self):
        Tk.destroy(self)
        #bring up the map again by sending message back to the control
        self.listener.open()

Both windows, the map and the level, open in the function GUI_Control.open(). Is there any way to make them open one at a time?

Upvotes: 1

Views: 470

Answers (1)

Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

In a Tkinter/tkinter application, you must have only one Tk() instance running at the same time. So to resolve your problem, you can simply use Toplevel().

Upvotes: 1

Related Questions