1
我想要製作一個遊戲,其中地圖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()中打開。有沒有辦法讓它們一次打開一個?