Tkinter的活动指标?

时间:2016-02-11 23:54:48

标签: python tkinter activity-indicator

每个操作系统都有一个活动指示器。 OS X和iOS有一朵花,可以点亮并以圆形图案淡化每个踏板。 Windows有一个旋转蓝盘的东西。 Android有一个灰色的东西(我认为它也可以有各种其他颜色 - IDK,我不会使用Android。)

在Tkinter中使用这些图标的最佳方式是什么?是否有一些内置的小部件提供此功能?是否有一个变量,我可以将一个Image小部件指向它以显示此图标(并为其设置动画?)

我知道Tkinter提供了一个进度条,它具有不确定的模式。我不想使用它 - 我需要适合小方块区域的东西,而不是长矩形区域。第一段中描述的活动指标将是完美的。

或者我最好的选择就是滚动自己的画布动画?

1 个答案:

答案 0 :(得分:0)

这应该足以满足您的要求。我想创建自己的动画或从你想要显示的帧逐帧下载。 Canvas更多的是与用户交互,这就是为什么我使用标签来显示图像......

import tkinter as tk

class Activity(tk.Label):
    def __init__(self, master = None, delay = 1000, cnf = {}, **kw):
        self._taskID = None
        self.delay = delay
        return super().__init__(master, cnf, **kw)

    # starts the animation
    def start(self):
        self.after(0, self._loop)

    # calls its self after a specific <delay>
    def _loop(self):
        currentText = self["text"] + "."

        if currentText == "....":
            currentText = ""

        self["text"] = currentText

        self._taskID = self.after(self.delay, self._loop)

    # stopps the loop method calling its self
    def stop(self):
        self.after_cancel(self._taskID)
        # Depends if you want to destroy the widget after the loop has stopped
        self.destroy()


class AcitivityImage(Activity):
    def __init__(self, imagename, frames, filetype, master = None, delay = 1000, cnf = {}, **kw):
        self._frames = []
        self._index = 0
        for i in range(frames):
            self._frames.append(tk.PhotoImage(file = imagename+str(i)+'.'+str(filetype)))

        return super().__init__(master, delay, cnf, **kw)

    def _loop(self):
        self["image"] = self._frames[self._index]

        # add one to index if the index is less then the amount of frames
        self._index = (self._index + 1 )% len(self._frames)

        self._taskID = self.after(self.delay, self._loop)


root = tk.Tk()
root.geometry("500x500")

# create a activity image widget
#root.b = AcitivityImage("activity", 3, "png", root)
#root.b.pack()

# create the activity widget
root.a = Activity(root, 500, bg = "yellow")
root.a.pack()

# start the loop
root.a.start()
#root.b.start()

# stop the activity loop after 7 seconds
root.after(7000, root.a.stop)
#root.after(8000, root.b.stop)

root.mainloop().
相关问题