如何通过按下按钮来修改图像

时间:2014-04-05 03:25:05

标签: python tkinter

我正在制作一个介绍序列,显示下面带有文字的图像。当按下“下一个”按钮时,图像应该会改变。当我按下按钮时,它会给我这个错误:

raceback (most recent call last):
  File "C:\Users\Isaac\Desktop\code\lib\lib-tk\Tkinter.py", line 1470, in __call__
    return self.func(*args)
  File "C:\Users\Isaac\Desktop\code\Programs\Newlife (concept)\Newlife.py", line 15, in next
    pic1=PhotoImage(file=img)
  File "C:\Users\Isaac\Desktop\code\lib\lib-tk\Tkinter.py", line 3306, in __init__
    Image.__init__(self, 'photo', name, cnf, master, **kw)
  File "C:\Users\Isaac\Desktop\code\lib\lib-tk\Tkinter.py", line 3262, in __init__
    self.tk.call(('image', 'create', imgtype, name,) + options)
TclError: couldn't open "pic3.gif": no such file or directory

请注意;按下按钮3次后会发生这种情况。我只想按一次。

我的代码:

    from Tkinter import *

Intro= [
    "In the year 2164 Humanity killed itself.",
    ]
pic=0

def next():
    global pic
    pic+=1
    img='pic'+str(pic)+'.gif'
    pic1=PhotoImage(file=img)
    image.create_image(0,0,image=pic1,anchor=NW)

intro_scene = Tk()
intro_scene.title('Destoyed')


text=Label(intro_scene,text=Intro[0],height=2)
image=Canvas(width=300,height=200)

Continue=Button(intro_scene,text='continue',bg='green',bd=3,width=10,command=next)
back=Button(intro_scene,text='back',bg='red',bd=3,width=5)

pic1=PhotoImage(file='pic1.gif')
image.create_image(0,0,image=pic1,anchor=NW)

image.pack(side='top'),text.pack(),back.pack(side='left'),Continue.pack(side='right')
intro_scene.mainloop()

我已经检查过整个next()函数是这样运行的:

def next():
    global pic
    print 'okay1'
    pic+=1
    print 'okay2'
    img='pic'+str(pic)+'.gif'
    print 'okay3'
    pic1=PhotoImage(file=img)
    print 'okay4'
    image.create_image(0,0,image=pic1,anchor=NW)
    print 'okay5'

一切都被打印出来。

我的图片采用GIF格式,名为pic1,pic2,pic3等。第一张图片在启动时打开。

0 个答案:

没有答案