gpt4 book ai didi

python - Tkinter 图像查看器方法

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

我是一名大气科学家,正在努力使用 Python(2.7 使用 PIL 和 Tkinter)来尝试创建一个简单的图像查看器来显示我们最终生成的一些预测产品。我想实现一个开始、停止、前进、后退和循环按钮。循环按钮及其关联的回调当前工作正常。循环方法归功于 Glenn Pepper(通过 google 找到了一个开源项目)。这是代码:

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 上搜索了项目、google 和 stackoverflow,但我不确定如何使这五种方法能够很好地配合使用。谁能分享一些关于我如何构建其他四种方法以便它们正常工作的指示?感谢您的宝贵时间!

最佳答案

我会将当前循环函数替换为类似以下内容(未经测试)。变化:添加一些全局名称以在函数之间共享数据;假设 current_image 有效,则使 change_image 执行更改图像所需的所有操作;除起始值外,使current_image在更改时始终为有效的图像编号;将forward()从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)

关于python - Tkinter 图像查看器方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27297814/

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