Python Tkinter中的活动指示器?

Python Tkinter中的活动指示器?,python,tkinter,activity-indicator,Python,Tkinter,Activity Indicator,每个操作系统都有一个活动指示器。OSX和iOS有一个花,它以圆形模式点亮和淡出每个踏板。Windows有一个旋转的蓝色磁盘。Android有一个灰色的磁盘(我认为它也可以有多种其他颜色——IDK,我不太使用Android) 在Tkinter中使用这些图标的最佳方式是什么?是否有一些内置的小部件提供这种功能?是否有一个变量,我可以指向一个图像小部件,让它显示这个图标(并设置动画?) 我知道Tkinter提供了一个进度条,它有一个不确定的模式。我不想用那个——我需要一个适合小正方形区域的东西,而不是

每个操作系统都有一个活动指示器。OSX和iOS有一个花,它以圆形模式点亮和淡出每个踏板。Windows有一个旋转的蓝色磁盘。Android有一个灰色的磁盘(我认为它也可以有多种其他颜色——IDK,我不太使用Android)

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

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


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

这应该足以满足您的需要。我想创建自己的动画,或者从互联网上逐帧下载您想要显示的动画。画布更适合与用户交互,这就是为什么我使用标签来显示图像

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().
将tkinter作为tk导入
课堂活动(tk.标签):
def uuu init uuuu(self,master=None,delay=1000,cnf={},**kw):
self.\u taskID=None
self.delay=延迟
返回super()。\uuuu init\uuuuu(主控,cnf,**千瓦)
#启动动画
def启动(自):
self.after(0,self.\u循环)
#在特定的
def_回路(自):
currentText=self[“文本”]+”
如果currentText==“…”:
currentText=“”
self[“text”]=当前文本
self.\u taskID=self.after(self.delay,self.\u循环)
#停止循环方法调用其自身
def停止(自):
self.after\u cancel(self.\u taskID)
#取决于循环停止后是否要销毁小部件
自我毁灭
课堂活动图像(活动):
def u uu init uuuuuu(self,imagename,frames,filetype,master=None,delay=1000,cnf={},**kw):
self._frames=[]
自身指数=0
对于范围内的i(帧):
self._frames.append(tk.PhotoImage(file=imagename+str(i)+'.+str(filetype)))
返回super()。\uuuu init\uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu
def_回路(自):
self[“image”]=self.\u帧[self.\u索引]
#如果索引小于帧数,则向索引添加一个
self.\u索引=(self.\u索引+1)%len(self.\u帧)
self.\u taskID=self.after(self.delay,self.\u循环)
root=tk.tk()
根几何(“500x500”)
#创建活动图像小部件
#root.b=AcitivityImage(“活动”,3,“png”,root)
#root.b.pack()
#创建活动小部件
root.a=活动(root,500,bg=“黄色”)
root.a.pack()
#开始循环
root.a.start()
#root.b.start()
#7秒后停止活动循环
root.after(7000,root.a.stop)
#root.after(8000,root.b.stop)
root.mainloop()。

不是答案,但这可能对MacOS有所帮助。您可能需要使用
画布
标签
显示图像,但您必须自己制作动画(使用
root.after
)@noelsecura-Huh。这依赖于
AppKit
/
Cocoa
。有没有办法将来自
AppKit
的小部件与来自
Tkinter
的小部件混合使用?我觉得你不能。是的,你是对的,Appkit将生成一个新窗口,也许有一种方法可以获得动画资源,但在这一点上,我不知道它比下载gif并在画布上自己制作动画有多好。