如何在 Tkinter 文本小部件中创建 "loading wheel"?

How to create a "loading wheel" in a Tkinter Text widget?

如何在 Tkinter Text Widget 中创建加载轮? 例如运行 通过 -、\、|、/ 的顺序创建旋转条的错觉。

我能找到的唯一示例是 commandline/console 应用程序

使用 console.write('text here') 将文本插入控制台 如果您希望在行尾旋转 bar/wheel,只需添加附加参数 console.write('text here', 'loader')

当您再写一行文字时,纺车会停止并消失。

class Console(Frame):
    def __init__(self, master, **kwargs):
        Frame.__init__(self, master)
        self.text = Text(self, wrap='word', **kwargs)
        self.text.pack()
        self.text.config(state='disabled')
        self.sequence = ['-', '\', '|', '/']
        self.load = False
        self.queue = Queue.Queue()
        self.update_me()
    def write(self, line, link=None):
        self.queue.put((line,link))
    def clear(self):
        self.queue.put((None, None))
    def update_me(self):
        try:
            while 1:
                line, link = self.queue.get_nowait()

                self.text.config(state='normal')
                if line is None:
                    self.text.delete(1.0, END)
                elif link and link == 'loader':
                    self.load = True
                    self.text.delete(self.text.index("end-2c"))
                    self.text.insert(self.text.index("end-1c"), str(line))
                else:
                    if self.load:
                        self.text.delete(self.text.index("end-2c"))
                        self.text.insert(self.text.index("end-1c"), str(line))
                    else:
                        self.text.insert(END, str(line))
                    self.load = False
                self.text.see(END)
                self.update_idletasks()
                self.text.config(state='disabled')
        except Queue.Empty:
            pass
        self.after(100, self.update_me)
        if self.load:
            self.queue.put((self.sequence[0], 'loader'))
            self.sequence.append(self.sequence.pop(0))

if __name__ == '__main__':
    # testing application
    import time
    root = Tk()
    console = Console(root)
    console.pack()

    def load_it():
        console.write('Loading World...', 'loader')
        time.sleep(3)
        console.write('Done')

    import threading
    t = threading.Thread(target=load_it)
    t.daemon = True
    t.start()

    root.mainloop()
    exit()