Python tkinter slider 控件

标签 python tkinter

我想使用 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/

相关文章:

python - Tkinter 'module' 对象不可调用

python - 为什么我的(增量)值呈指数级增长?

python - 突出显示 pandas df 中列的 nmaxest 值

python - 模块 'sys' 没有 '_MEIPASS' 成员

python - 如何在BASH中自动添加多个文件?

python - matplotlib 导航栏错误 'FigureCanvasTkAgg' 对象在 tkinter 中没有属性 'manager'

python - 在 Python 中创建空白图像(允许逐像素操作)

python - 如何构建具有多个文件和内部依赖项的项目并上传到 PyPI

python - 如何将 lambda 应用程序作为本地 API 运行

python - Tkinter。使用 "different"命令函数创建多个按钮