我是一名大气科学家,通过python(2.7使用PIL和Tkinter)努力创建一个简单的图像查看器,它将显示我们最终生成的一些预测产品。我想实现一个启动,停止,前进,后退和循环按钮。循环按钮及其关联的回调当前正常工作。循环方法归功于Glenn Pepper(通过谷歌找到了一个开源项目)。 这是代码:
from Tkinter import *
from PIL import Image, ImageTk
import tkMessageBox
import tkFileDialog
#............................Button Callbacks.............................#
root = Tk()
root.title("WxViewer")
root.geometry("1500x820+0+0")
def loop():
StaticFrame = []
for i in range(0,2):
fileName="filename"+str(i)+".gif"
StaticFrame+=[PhotoImage(file=fileName)]
def animation(currentframe):
def change_image():
displayFrame.create_image(0,0,anchor=NW,
image=StaticFrame[currentframe], tag='Animate')
# Delete the current picture if one exist
displayFrame.delete('Animate')
try:
change_image()
except IndexError:
# When you get to the end of the list of images -
#it simply resets itself back to zero and then we start again
currentframe = 0
change_image()
displayFrame.update_idletasks() #Force redraw
currentframe = currentframe + 1
# Call loop again to keep the animation running in a continuous loop
root.after(1000, animation, currentframe)
# Start the animation loop just after the Tkinter loop begins
root.after(10, animation, 0)
def back():
print("click!")
def stop():
print("click!")
def play():
print("click!")
def forward():
print("click!")
#..........................ToolFrame Creation............................#
toolFrame = Frame(root)
toolFrame.config(bg="gray40")
toolFrame.grid(column=0,row=0, sticky=(N,W,E,S) )
toolFrame.columnconfigure(0, weight = 1)
toolFrame.rowconfigure(0, weight = 1)
toolFrame.pack(pady = 0, padx = 10)
backButton = Button(toolFrame, text="Back", command = back)
backButton.pack(side = LEFT)
stopButton = Button(toolFrame, text = "Stop", command = stop)
stopButton.pack(side = LEFT)
playButton = Button(toolFrame, text = "Play", command = play)
playButton.pack(side = LEFT)
forwardButton = Button(toolFrame, text = "Forward", command = forward)
forwardButton.pack(side = LEFT)
loopButton = Button(toolFrame, text = "Loop", command = loop)
loopButton.pack(side = LEFT)
toolFrame.pack(side = TOP, fill=X)
#........................DisplayFrame Creation..........................#
displayFrame = Canvas(root, width=1024,height=768)
displayFrame.config(bg="white")
displayFrame.grid(column=0,row=0, sticky=(N,W,E,S) )
displayFrame.columnconfigure(0, weight = 1)
displayFrame.rowconfigure(0, weight = 1)
displayFrame.pack(pady = 5, padx = 10)
displayFrame.pack(side = LEFT, fill=BOTH)
#...............................Execution...............................#
root.mainloop()
这很简单。我已经搜索了GitHub的项目,谷歌和stackoverflow,但我不确定如何让这五种方法相互配合。任何人都可以分享一些关于如何构建其他四种方法以使它们正常工作的指针吗?谢谢你的时间!
答案 0 :(得分:2)
我会用以下内容替换当前的循环函数(未经测试)。更改:添加一些全局名称以在函数之间共享数据;如果current_image有效,make change_image会执行更改图像所需的所有操作;除起始值外,make current_image在更改时始终是有效的图像编号; factor forward()out of animate()(只是重复向前调用)。
n_images = 2
images = [PhotoImage(file="filename"+str(i)+".gif") for i in range(n_images)]
current_image = -1
def change_image():
displayFrame.delete('Animate')
displayFrame.create_image(0,0, anchor=NW,
image=StaticFrame[current_image], tag='Animate')
displayFrame.update_idletasks() #Force redraw
callback = None
def animate():
global callback
forward()
callback = root.after(1000, animate)
以下是其他三个功能。
def forward():
global current_image
current_image += 1
if current_image >= n_images:
current_image = 0
change_image()
def back():
global current_image
current_image -= 1
if current_image < 0:
current_image = n_images-1
change_image()
def stop():
if callback is not None:
root.after_cancel(callback)