将多个PhotoImage添加到TKinter Canvas

时间:2016-03-29 16:31:02

标签: image python-2.7 tkinter tkinter-canvas

我遇到了一个问题,让我觉得我一直在撞墙。我正在尝试将多个(2个用于初学者)的photoimages添加到tkinter画布中。以下是代码:

def build_frame(screenHeight, screenWidth):
    canvas = Canvas(root, bg = 'blue', height = screenHeight, width = screenWidth)

    player = create_random_player()
    enemy = create_random_enemy()
    display_player_image(canvas, player)
    display_enemy_image(canvas, enemy)

    bind_all_keys(canvas, player)

    canvas.pack()


def display_player_image(canvas, player):
    tkImage = ImageTk.PhotoImage(Image.open(player.playerImgPath))
    player.playerId = canvas.create_image(player.playerPositionX, player.playerPositionY, image = tkImage, anchor = NE)
    player.playerImg = tkImage


def display_enemy_image(canvas, enemy):
    tkImage = ImageTk.PhotoImage(Image.open(enemy.enemyImgPath))
    enemy.enemyId = canvas.create_image(enemy.enemyPositionX, enemy.enemyPositionY, image = tkImage, anchor = NE)
    enemy.enemyImg = tkImage


def create_random_player():
    return Player(0, randint(0,9), randint(0,9), randint(0,9), PLAYER_IMAGE_PATH, 50, 0)


def create_random_enemy():
    return Enemy(0, randint(0,9), randint(0,9), randint(0,9), ENEMY_IMAGE_PATH, 150, 150)

我确保存储了对图像的引用,并且我知道图像不在同一位置,但我无法理解为什么第二个图像没有出现。我甚至放入打印语句进行检查,以确保正在创建图像并且它位于给定的坐标以及可以用来识别它的ID。

0 个答案:

没有答案