在方法内部创建动画

2024-10-03 23:18:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个名为compare image的方法,它将保存的文件和摄像头捕获的人脸进行比较

有时候需要很长时间来处理, 我希望我可以使用像加载条这样的动画,或者我可以使用.gif文件 直到所有过程完成

这是我的代码示例

 def compareimage(id):
    try:
      loadimage()
      compareimage()
      insertlogintodatatabase()
      savelogimage()
    except:
      exception()

我希望我能在整个过程中创作出动画

如何创建它

在这里输入代码


Tags: 文件方法代码imageid示例过程def
1条回答
网友
1楼 · 发布于 2024-10-03 23:18:36

我不太明白你所说的“动画”是什么意思,因为它可能意味着很多事情。如果您试图在tkinter中创建一个进度条,那么导入tkinter.ttk就可以了!确定进度条的代码:

from tkinter import *
from tkinter import ttk

root = Tk()
root.geometry("400x300")
prg = ttk.Progressbar(root, orient=HORIZONTAL, length=300, mode='determinate')
prg.pack(pady=20)
# You can use the start function to animate the progress bar just give it an int to determine the speed!
prg.start(10)
# You can also use the two lines below to add to the progress bar's value
# prg.step(5)
# prg['value'] = 50
root.mainloop()

您还可以将模式设置为“不确定”,然后启动进度条,这可能是您想要的。如果你说的是自定义动画,那就有点不同了。如果 如果要在tkinter中创建自定义进度条,则应使用画布小部件。tkinter中使用画布的自定义进度条示例:

from tkinter import *

root = Tk()
root.geometry("400x400")
root.config(bg="black")
percent = 0
progress = None
canvas = Canvas(root, width=310, height=10, bg="black", highlightcolor="black", highlightbackground="black")
canvas.pack(pady=10)
rect = canvas.create_rectangle(5, 0, 300, 6, fill="#2C383E", outline="#2C383E")


def animate():
    global percent, progress
    if percent < 100:
        percent += 1
        if progress is not None:
            canvas.delete(progress)
        progress = canvas.create_rectangle(5, 0, percent * 3, 6, fill="#3333ff", outline="#3333ff")
    # Remove these two lines (the elif statement) to make the progress bar 'determinate'
    elif percent == 100:
        percent = 0
    root.after(5, animate)


# You can just call the function or assign it to a button
# animate()
btn = Button(root, text="Fill! ", command=animate)
btn.pack()
root.mainloop()

相关问题 更多 >