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

标签 python tkinter loading

如何在 Tkinter 文本小部件中创建加载轮? 例如运行 -、\、|、/序列以创建旋转条的错觉。

我能找到的唯一示例是命令行/控制台应用程序

最佳答案

使用 console.write('text here') 将文本插入控制台 如果您希望在行尾有一个旋转条/轮,只需添加附加参数 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()

关于python - 如何在 Tkinter 文本小部件中创建 "loading wheel"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29187387/

相关文章:

python - 如何将此列表转换为 Python 3.7 中的有效 json 对象?

python - 在 Windows 中安装 eyed3

python - _tkinter TclError : can't find package Tix

java - 用于加载 PNG 图像的替代库

javascript - 无法让我的 javascript 工作

python - 动态规划 - 洗车

python - 如何将多个数据框列添加到基本 mplfinance 图中()

python - 绘制和更新车速表指针

python - 使用cxfreeze时无法使用tkinter/python脚本设置值

database - 预加载数据的好方法是什么?