Python:启动程序时图像不显示

时间:2014-10-13 17:20:01

标签: python

当我跑这个时,我的精灵图像不显示

from tkinter import *
import sys, time, os

moveSpeed = 5
#stops error with holding keys down
sys.setrecursionlimit(10000)
X = 396
Y = 281
def cen(name, w = 500, h = 100):
    ws = name.winfo_screenwidth()
    hs = name.winfo_screenheight()
    # calculate position x, y
    x = (ws/2) - (w/2)
    y = (hs/2) - (h/2)
    name.geometry('%dx%d+%d+%d' % (w, h, x, y))
def up(event):
    global Y, X
    #checks if in play area then moves by the amount set
    if Y > 0:
        print("Y = ",Y)
        Y-= moveSpeed
        X = X
        sprite.place(x = X, y = Y)
        game.mainloop()
def down(event):
    global Y, X
    #checks if in play area then moves by the amount set
    if Y < 421:
        print("Y = ",Y)
        Y+= moveSpeed
        X = X
        sprite.place(x = X, y = Y)
        game.mainloop()
def left(event):
    global Y, X
    #checks if in play area then moves by the amount set
    if X > -10:
        print("X = ",X)
        Y = Y
        X-= moveSpeed
        sprite.place(x = X, y = Y)
        game.mainloop()
def right(event):
    global Y, X
    #checks if in play area then moves by the amount set
    if X < 431:
        print("X = ",X)
        Y= Y
        X+= moveSpeed
        sprite.place(x = X, y = Y)
        game.mainloop()
game = Tk()
cen(game, 500, 500)
backgroundImage = PhotoImage(file="background.png")
background = Label(game, image=backgroundImage).pack()
spriteture = PhotoImage(file="sprite.gif")
sprite = Label(game, image=spriteture)
sprite.place(x = X, y = Y)
sprite.pack()
game.bind('<Up>',        up)
game.bind('<Down>',  down)
game.bind('<Left>',  left)
game.bind('<Right>',  right)
game.mainloop()

当我开始这个游戏时,它会让图像在我按箭头键之前不会开始可见 当你按箭头时它会移动精灵 它没有给我任何错误,所以我很困惑 请帮忙

1 个答案:

答案 0 :(得分:0)

问题是,当我不需要

时,我正在打包图像

所以我需要摆脱

sprite.pack()