如何在另一个任务旁边运行tkinter的after事件?

时间:2019-06-07 10:26:49

标签: python tkinter

我正在尝试为执行某些任务的脚本创建tkinter GUI。通过单击开始按钮来触发任务,我想通过显示以下内容来添加动态标签,以显示任务“正在进行中”: “工作。” →“工作中..”→“工作中...”

我参考了this帖子并编写了以下脚本。在这里,我使用进度条来表示我的“任务”,并且期望状态标签在更新进度条时会发生变化(如上所述)。

import tkinter as tk

class UI:
    def __init__(self):
        self.root = tk.Tk()
        self.root.title('Hello World')

        self.prog_Label = tk.Label(self.root, text='Progress')
        self.prog_Label.grid(row=0, column=0, sticky=tk.W, padx=20, pady=(10, 0))

        self.prog_Bar = tk.ttk.Progressbar(self.root)
        self.prog_Bar.configure(value=0, mode='determinate', orient='horizontal')

        self.prog_Bar.grid(row=1, column=0, sticky=(tk.W, tk.E, tk.N, tk.S), padx=20, pady=5)

        self.exe_Btn = tk.Button(self.root, text='Start', padx=15, command=self.run, relief='groove')
        self.exe_Btn.grid(row=2, column=0, padx=80, pady=(40, 20), sticky=tk.E)

        self.prog_Label = tk.Label(self.root, text='Status:-')
        self.prog_Label.grid(row=3, column=0, sticky=tk.W, padx=20, pady=10)

        self.root.mainloop()

    def run(self):
        self.update_status('Working')
        n = 0
        self.prog_Bar.configure(value=n, maximum=100000, mode='determinate', orient='horizontal')
        for i in range(100000):
            n += 1
            self.prog_Bar.configure(value=n)
            self.prog_Bar.update_idletasks()

    def update_status(self, status=None):
        if status is not None:
            current_status = 'Status: ' + status
        else:
            current_status = self.prog_Label['text']
            if current_status.endswith('...'):
                current_status = current_status.replace('...', '')
            else:
                current_status += '.'

        self.prog_Label['text'] = current_status
        self.prog_Label.update_idletasks()
        self._status = self.root.after(1000, self.update_status)

if __name__ == '__main__':
    ui = UI()

但是,该程序的行为方式是:单击“开始”按钮时,尽管状态标签立即从“-”变为“正在工作”,但它仅在进度条到达末尾时才开始添加点。

是否可以修改它以实现我的目的?

1 个答案:

答案 0 :(得分:1)

我对您的结构进行了一些更改,因此您的任务现在位于其自己的类中,而无需睡觉,您可以在那里执行任务。我为该任务添加了一个线程,因为我认为它需要自己的进程,这会停止应用程序冻结,因为它将阻塞主UI循环。

import threading
import time

import tkinter as tk
import tkinter.ttk as ttk

class Task:
    def __init__(self):
        self.percent_done = 0

        threading.Thread(target = self.run).start()

    def run(self):
        while self.percent_done < 1:
            self.percent_done += 0.1

            # Do your task here

            time.sleep(0.5)

        self.percent_done = 1


class Application():
    def __init__(self):
        self.root = tk.Tk()

        self.root.title("Window Title")

        self.task = None
        self.label_dots = 0

        self.prog_bar = tk.ttk.Progressbar(self.root)
        self.prog_bar.configure(value=0, maximum=100, mode='determinate', orient='horizontal')

        self.prog_bar.grid(row=1, column=0, sticky=(tk.W, tk.E, tk.N, tk.S), padx=20, pady=5)

        self.run_btn = tk.Button(self.root, text='Start', padx=15, command=self.start_task, relief='groove')
        self.run_btn.grid(row=2, column=0, padx=80, pady=(40, 20), sticky=tk.E)

        self.prog_label = tk.Label(self.root, text='Status: -')
        self.prog_label.grid(row=3, column=0, sticky=tk.W, padx=20, pady=10)

    def start_task(self):       
        self.task = Task()

        self.update_ui()


    def update_ui(self):
        # Percent is between 0 and 1 
        if 0 < self.task.percent_done < 1:
            status = "Working"

            self.label_dots += 1
            self.label_dots = self.label_dots % 4
        else:
            status = "Finished"

            self.label_dots = 0

        self.prog_bar.configure(value=self.task.percent_done * 100)

        label_text = "Status: - " + status + ("." * self.label_dots)

        self.prog_label.config(text = label_text)

        if status != "Finished":
            self.root.after(1000, self.update_ui)           


Application().root.mainloop()