python - 用线程在tkinter中的进度条

标签 python multithreading tkinter

你好,我有问题,在tkinter中有一个进度条,您将看到:
我执行了一个方法,我希望在执行此操作时出现进度条,问题是该方法结束时该条不会停止,我不知道该怎么做...请提供一些帮助

import threading
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        t = threading.Thread(target=self.contar)
        windows_bar = WinProgressBar(self)
        self.after(t.start())

    def contar(self):
        for i in range(1000000):
            print("está contando", i)
        return True


class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

最佳答案

您可以尝试在威胁中阻止它

def contar(self):
    for i in range(500):
        print("está contando", i)
    
    self.windows_bar.progressbar.stop()
    self.windows_bar.destroy()
    self.button.config(state=tk.NORMAL)
import threading
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        t = threading.Thread(target=self.contar)
        t.start()

        self.windows_bar = WinProgressBar(self)
        
    def contar(self):
        for i in range(500):
            print("está contando", i)

        print('stop')            
        self.windows_bar.progressbar.stop()
        self.windows_bar.destroy()
        self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

许多GUI框架不喜欢在线程中更改窗口小部件,然后可以使用变量self.running(将在两个线程之间共享)来通知程序线程是否正在运行。
def contar(self):
    self.running = True

    for i in range(500):
        print("está contando", i)

    self.running = False
在主线程中,您可以使用after()定期检查此变量以停止进度条和/或关闭窗口-这次主线程正在更改小部件。
def check_thread(self):
    if self.running:
        self.after(1000, self.check_thread) # run again after 1s (1000ms)
    else:
        print('stop')
        self.windows_bar.progressbar.stop()
        self.windows_bar.destroy()
        self.button.config(state=tk.NORMAL)
import threading
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        t = threading.Thread(target=self.contar)
        t.start()

        self.windows_bar = WinProgressBar(self)

        self.check_thread() # run first time
        
    def contar(self):
        self.running = True
        for i in range(500):
            print("está contando", i)
        self.running = False

    def check_thread(self):
        if self.running:
            self.after(1000, self.check_thread) # run again after 1s (1000ms)
        else:
            print('stop')
            self.windows_bar.progressbar.stop()
            self.windows_bar.destroy()
            self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

如果线程不能共享变量,则可以使用queue将信息从线程发送到主线程。
import threading
import queue
import tkinter as tk
import tkinter.ttk as ttk


class App(tk.Tk):
    def __init__(self):
        super().__init__()
        self.title("Progressbar example")
        self.button = tk.Button(self, text="Start", command=self.start_action)
        self.button.pack(padx=10, pady=10)

    def start_action(self):
        self.button.config(state=tk.DISABLED)
        
        self.q = queue.Queue()
        t = threading.Thread(target=self.contar, args=(self.q,))
        t.start()

        self.windows_bar = WinProgressBar(self)
        
        self.check_queue()
        
    def contar(self, q):
        for i in range(200):
            print("está contando", i)
        
        q.put('finished')
        
    def check_queue(self):
        if self.q.empty() or self.q.get() != 'finished':
            self.after(1000, self.check_queue)
        else:
            print('stop')
            self.windows_bar.progressbar.stop()
            self.windows_bar.destroy()
            self.button.config(state=tk.NORMAL)
        
        
class WinProgressBar(tk.Toplevel):
    def __init__(self, parent):
        super().__init__(parent)
        self.title("Barr progress")
        self.geometry("300x200")
        self.progressbar = ttk.Progressbar(self, mode="indeterminate")
        self.progressbar.place(x=30, y=60, width=200)
        self.progressbar.start(20)


if __name__ == "__main__":
    app = App()
    app.mainloop()

关于python - 用线程在tkinter中的进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63836979/

相关文章:

python - pytest stdout 从测试输出刷新到不同的文件

c# - 如何暂停创建新线程直到旧线程结束

c++ - 多线程和使用事件

python - 在 Tkinter 循环中跳出

python - 修复 Tkinter 中 RadioButtons 的设计

python - 属性错误: module 'keras.backend' has no attribute 'set_image_dim_ordering'

python - 删除 matplotlib 图中的空子图

.NET 开发人员使用 Python 还是 Ruby?

C 编程 - 线程,以及什么是 void (*func)(void*, unsigned long)

python - 无法在条目小部件上设置 focus()