只有一个人应该打开两个Windows

时间:2016-05-26 04:05:42

标签: python user-interface tkinter

我正在尝试制作一个游戏,其中地图Tk()窗口打开,玩家选择位置,地图窗口关闭,水平窗口打开。当玩家有选择时,离开关卡并选择“是”,关闭等级Tk(),地图应该打开,这样玩家可以点击其他位置并打开另一个Tk()。出于某种原因,所有Tks都会立即打开。这是我的代码。

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()

两个窗口,地图和关卡,在GUI_Control.open()函数中打开。有没有办法让它们一次打开一个?

1 个答案:

答案 0 :(得分:1)

Tkinter / tkinte r应用程序中,您必须同时运行一个Tk()实例。因此,要解决您的问题,您只需使用Toplevel()