0
以下TKinter代碼正在生成空TK窗口。我無法弄清楚爲什麼。我正在妥善包裝一切。地圖是應該出現的第一幀,但是當它啓動時,只有沒有任何圖像或按鈕的頂部欄。我也嘗試首先發射hydra水平框架,它會產生相同的結果。生成空TKinter GUI
#class inspired by sentdex SeaOfBTCapp
#see https://www.youtube.com/watch?v=A0gaXfM1UN0&index=2&list=PLQVvvaa0QuDclKx-QpC9wntnURXVJqLyk
class GUI_Control(Tk):
def __init__(self, player, control, *args, **kwargs):
self.player = player
self.delegate = control
Tk.__init__(self, *args, **kwargs)
self.window = Frame(self)
self.window.pack()
#specify frames that can be loaded into the TK window. These will be defined in subclasses of Tk.Frame
self.frames = {}
#setup initial frame, all frames take GUI_Control as a listener to control event handling with window changes
level_keys = [Map, Hydra_Level]
for level in level_keys:
frame = level(self.window, self)
self.frames[level] = frame
self.show(Map)
#show the frame from the dictionary in the tkinter window
def show(self, controller):
frame = self.frames[controller]
print(isinstance(frame, Frame))
frame.tkraise()
def open(self):
self.mainloop()
def switch(self):
pass
class Map(Frame):
def __init__(self, master, controller, *args, **kwargs):
Frame.__init__(self, master)
self.controller = controller
# define map gui here
self.map_picture = PhotoImage(file=r"images/archipelago.gif")
self.image = Label(self, image=self.map_picture)
self.image.pack(padx=10, pady=10)
self.go_to_button = Button(self, text="Go to", command=lambda: self.controller.show(Hydra_Level))
self.go_to_button.pack()
class Hydra_Level(Frame):
def __init__(self, master, controller, *args, **kwargs):
Frame.__init__(self, master)
self.listener = master
self.go_to_button = Button(self, text="Back to map", command=lambda: self.controller.show(Map))
self.go_to_button.grid(row=0)
你說得對。謝謝你糾正我 –