gpt4 book ai didi

python - TK 图像未出现

转载 作者:行者123 更新时间:2023-12-01 03:56:04 25 4
gpt4 key购买 nike

我正在尝试将图像放在 TKinter Canvas 上,并在图像下方放置其他按钮。由于某种原因我无法让这张照片出现。我还没有实现这些按钮。这是迄今为止我的代码。

class GUI_Control:
def __init__(self, player):
self.player = player
self.map = Tk()
self.MAP_WIDTH = 600
self.MAP_HEIGHT = 375
#define map gui here
self.canvas = Canvas(self.map, width=self.MAP_WIDTH, height=self.MAP_HEIGHT)
self.map_picture = PhotoImage(file=r"images/archipelago.gif")
self.canvas.create_image(0, 0, image=self.map_picture)
#define level gui's here

def open(self):
self.map.mainloop()

def hide_map(self):
self.map.destroy()


#debugging
if __name__ == "__main__":
gui = GUI_Control(Player.Player())
gui.open()

最佳答案

您需要使用 Tk 的几何管理机制之一来告诉它在其容器内的何处渲染 Canvas 。

最简单的方法是添加 self.canvas.pack() ,如下所示:

    #define map gui here
self.canvas = Canvas(self.map, 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)
#define level gui's here

关于python - TK 图像未出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37450334/

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