gpt4 book ai didi

Python tkinter slider 控件

转载 作者:行者123 更新时间:2023-12-01 04:02:15 26 4
gpt4 key购买 nike

我想使用 slider 控制图像动画的速度。因此,我尝试执行以下操作,但当我改变 slider 时,我得到许多图像重新出现在同一 Canvas 上。我想使用 slider 改变一张图像与下一张图像之间的时间间隔。

from tkinter import *
#import tkFont
import random
from time import sleep
root = Tk()

class App:

def __init__(self, master):
frame = Frame(master)
frame.pack()
scale = Scale(frame, from_=0, to=100, command=self.update)
scale.grid(row=0)

def update(self, duty):

#Importing the images. They are named a1.gif, a2.gif...a7.gif
frame=[]
for i in range(1,10):
fname="CORE\\a"+str(i)+".gif"
frame+=[PhotoImage(file=fname)]
wrap = Canvas(root, width=200, height=140)
wrap.pack()

def do_animation(currentframe):
def do_image():
wrap.create_image(100,70,image=frame[currentframe], tag='ani')
# Delete the current picture if one exists
wrap.delete('ani')
try:
do_image()
except IndexError:
# End of image list reached, start over at the first image
#- works for an arbitrary number of images
currentframe = 0
do_image()
wrap.update_idletasks() #Force redraw
currentframe = currentframe + 1
# Call myself again to keep the animation running in a loop
root.after(100, do_animation, currentframe)
# Start the animation loop just after the Tkinter loop begins
root.after(100, do_animation, 0)

app = App(root)
#app.geometry("800x480")
root.mainloop()
python

最佳答案

您每次都会创建一个新 Canvas ,而不是使用 slider 的速度。我想这就是你想要的。

from tkinter import *
#import tkFont
import random
from time import sleep
root = Tk()

class App:

def __init__(self, master):
frame = Frame(master)
frame.pack()
self.scale = Scale(frame, from_=0, to=1000)
self.scale.grid(row=0)
self.wrap = Canvas(root, width=200, height=140)
self.wrap.pack()
self.update()

def update(self):

#Importing the images. They are named a1.gif, a2.gif...a7.gif
frame=[]
for i in range(1,10):
fname="CORE\\a"+str(i)+".gif"
frame+=[PhotoImage(file=fname)]

def do_animation(currentframe):

def do_image():

self.wrap.create_image(100,70,image=frame[currentframe], tag='ani')
# Delete the current picture if one exists
self.wrap.delete('ani')
try:
do_image()
except IndexError:
# End of image list reached, start over at the first image
#- works for an arbitrary number of images
currentframe = 0
do_image()
self.wrap.update_idletasks() #Force redraw
currentframe = currentframe + 1
# Call myself again to keep the animation running in a loop
root.after(self.scale.get(), do_animation, currentframe)
# Start the animation loop just after the Tkinter loop begins
root.after(100, do_animation, 0)

app = App(root)
#app.geometry("800x480")
root.mainloop()

关于Python tkinter slider 控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36270802/

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