gpt4 book ai didi

python - 只应打开一扇 window 时却打开了两扇 window

转载 作者:太空宇宙 更新时间:2023-11-03 16:32:52 24 4
gpt4 key购买 nike

我正在尝试制作一个游戏,其中 map Tk() 窗口打开,玩家选择位置, map 窗口关闭,关卡窗口打开。当玩家选择离开关卡并选择"is"时,关卡 Tk() 应该关闭, map 应该重新打开,以便玩家可以单击不同的位置并打开另一个 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()

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

最佳答案

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

关于python - 只应打开一扇 window 时却打开了两扇 window ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451397/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com