从其他应用程序调用时,tkinter进度条不会更新

时间:2018-04-10 13:00:00

标签: python python-3.x tkinter ttk

我正在开发一个带有tkinter的GUI来管理数据库中的图像(导入文件,加载文件,查询......)

当扫描新目录及其子目录以将新图像放入数据库时​​,将启动专用GUI: 它由一个Text小部件和一个显示扫描进度的进度条组成,其中打印了当前分析的目录的名称。 当我单独调用此GUI时,只要我在进度条中的每次更改后使用update(),进度条就会更新并正确进行。另一方面, 即使我不使用更新,Text小部件也会正确更新。

但是,当我从主GUI调用它时,进度条不会更新,而Text小部件正确更新。

我希望有人可以提供帮助!

以下是进度条GUI的代码。我使用的是Python 3.6。

from tkinter.filedialog import *
from tkinter.ttk import *


class ScanDirectoryForJPG(Tk):
    """
    Inherited from the Tk class
    """

    def __init__(self, parent, Path=None):
        Tk.__init__(self, parent)
        self.parent = parent
        self.PathDicom = Path
        if self.Path == None:
            self.Path = askdirectory(title='Select a directory to scan')

        self.title('Scan {} for JPG files'.format(self.Path))
        self.status_string = 'Scanning the content of {} folder\n'.format(self.Path)
        self.initialize_gui()
        self.scan_directory()

    def initialize_gui(self):

        # Style
        self.style = Style()
        self.style.theme_use('vista')

        # Main window
        self.grid()
        self.grid_columnconfigure([0], weight=1)
        self.grid_rowconfigure([0], weight=1)

        # Status
        self.status_label = Text(self)
        self.status_label.grid(row=0, column=0, sticky='NSEW')
        self.status_label.insert(END, 'Looking for JPG files in {}\n'.format(self.Path))

        # Progress Bar
        self.p = DoubleVar()
        self.progress_bar = Progressbar(self, orient='horizontal', mode='determinate', variable=self.p, maximum=100)
        self.p.set(0)
        self.progress_bar.grid(row=1, column=0, rowspan=1, sticky='EW')

    def scan_directory(self):
        """

        """
        number_of_files = sum([len(files) for r, d, files in os.walk(self.Path)])
        count = 0

        for dirName, subdirList, fileList in os.walk(self.Path):
            self.status_label.insert(END, '\t-exploring: {}\n'.format(dirName))
            self.update()

            for filename in fileList:
                count += 1
                value = count / number_of_files * self.progress_bar['maximum']
                if value >= (self.progress_bar['value'] + 1):
                    # update the progress bar only when its value is increased by at least 1 (avoid too much updates of the progressbar)
                    self.p.set(self.progress_bar['value'] + 1)
                    self.update()

                file = os.path.join(dirName, filename)

                # if the file is a JPG, load it into the database
                # ...
                # ...
                # ..

        self.status_label.insert(END, 'FINISH\n')
        self.update()


if __name__ == '__main__':
    app = ScanDirectoryForJPG(None, Path='D:\Data\Test')
    app.mainloop()
    print('App closed')

1 个答案:

答案 0 :(得分:2)

如果你必须在tkinter中调用update(),那你就错了。

在这种情况下,您有一个遍历目录树的循环。在整个时间内,您的代码都在该循环内,您不会立即处理事件,因此您必须始终致电update()

相反,捕获os.walk的输出或者只是收集顶层目录内容,然后使用after一次处理一个项目,传递迭代器或列表,这样一旦处理完单个项目,您再次打电话来处理下一个。这样,mainloop将及时处理UI事件,并将目录树处理作为事件排队等等。一旦你以这种方式重新编写应用程序,你应该更好地响应应用程序。

实施例

为了证明这一点,os.walk返回一个生成器,以便我们可以使用事件后调度每个目录,因为next(generator)的每次调用都会产生包含其文件的下一个目录。

要监控进度,我们需要一些方法来计算要访问的目录或文件的数量,以及此演示是否用于整个文件系统,该应用程序似乎将冻结。这也可以分解为基于事件的代码,以防止这种影响。

我使用after(10, ...)让它显示效果,但为了获得最大速度,请改用after_idle

import sys
import os
import tkinter as tk
import tkinter.ttk as ttk
from tkinter.filedialog import askdirectory

class App(ttk.Frame):
    def __init__(self, parent, title):
        #tk.Frame.__init__(self, parent)
        super(App, self).__init__(parent)
        parent.wm_withdraw()
        parent.wm_title(title)
        self.create_ui()
        self.grid(sticky = "news")
        parent.wm_protocol("WM_DELETE_WINDOW", self.on_destroy)
        parent.grid_rowconfigure(0, weight=1)
        parent.grid_columnconfigure(0, weight=1)
        parent.wm_deiconify()

    def create_ui(self):
        textframe = ttk.Frame(self)
        self.text = text = tk.Text(textframe)
        vs = ttk.Scrollbar(textframe, orient=tk.VERTICAL, command=text.yview)
        text.configure(yscrollcommand=vs.set)
        text.grid(row=0, column=0, sticky=tk.NSEW)
        vs.grid(row=0, column=1, sticky=tk.NS)

        textframe.grid_columnconfigure(0, weight=1)
        textframe.grid_rowconfigure(0, weight=1)
        textframe.grid(row=0, column=0, columnspan=2, sticky=tk.NSEW)
        self.progressvar = tk.IntVar()
        self.progress = ttk.Progressbar(self, variable=self.progressvar)
        test_button = ttk.Button(self, text="Walk", command=self.on_walk)
        exit_button = ttk.Button(self, text="Exit", command=self.on_destroy)
        self.progress.grid(row=1, column=0, sticky=tk.NSEW)
        test_button.grid(row=1, column=0, sticky=tk.SE)
        exit_button.grid(row=1, column=1, sticky=tk.SE)
        self.grid_rowconfigure(0, weight=1)
        self.grid_columnconfigure(0, weight=1)

    def on_destroy(self):
        self.master.destroy()

    def on_walk(self):
        root = askdirectory()
        self.walk(root)

    def walk(self, root=None):
        if root:
            # this is potentially costly, but how to find the number of files to be examined?
            count = sum([len(files) for (root,dirs,files) in os.walk(root)])
            self.text.delete("1.0", "end")
            self.progress.configure(maximum=count)
            self.progressvar.set(0)
            walker = os.walk(root)
            self.after(100, self.do_one, walker)

    def do_one(self, walker):
        try:
            root,dirs,files = next(walker)
            for file in files:
                self.text.insert(tk.END, os.path.join(root, file), "PATH", "\n", "")
                self.text.see(tk.END)
                self.progressvar.set(self.progressvar.get() + 1)
            self.after(10, self.do_one, walker)
        except StopIteration:
            pass


def main(args):
    root = tk.Tk()
    app = App(root, "Walk directory tree")
    root.mainloop()

if __name__ == '__main__':
    sys.exit(main(sys.argv))