python - Python 2.7 中的 Tkinter ProgressBar 和 Os.Walk

标签 python python-2.7 tkinter progress-bar os.walk

我有一个工作 GUI,带有一个不稳定的进度条。我的问题似乎是我无法让进度条根据源目录中的项目数量正确进行。有什么想法吗?

def Progress(self):
    progress = Frame(self)
    progress.pack(fill=X)
    self.progressLine = ttk.Progressbar(progress, orient=HORIZONTAL, length=550, mode='determinate', maximum=100)
    self.progressLine.pack(pady=5)

def Scatter(self):
    self.t_start.delete('1.0', END)
    if Entry.get(self.sourceE) > 0 or Entry.get(self.destE) > 0:
        if not os.path.exists(Entry.get(self.sourceE)) or not os.path.exists(Entry.get(self.destE)):
            self.t_start.delete('1.0', END)
            self.t_start.insert(END, '*WARNING* Missing Source and/or Destination Folders')
            return

    self.t_start.delete('1.0', END)
    self.t_start.insert(END, "Counting Assets\n")
    self.totalFiles = len(list(os.walk(os.path.abspath(Entry.get(self.sourceE)))))
    self.stepFiles = (100/self.totalFiles)
    self.progressLine['value'] = self.stepFiles
    self.t_start.insert(END, "Process Started...\n")
    #self.progressLine.step(self.stepFiles)

    for root, subFolders, files in os.walk(Entry.get(self.sourceE)):
        self.progressLine.step(self.stepFiles)
        for file in files:
            if not file.startswith('.'):
                subFolder = os.path.join(Entry.get(self.destE), file[:1], file[:2], file [:3], file [:4])
                checkFile = os.path.join(subFolder, file)
                if not os.path.isdir(subFolder):
                    os.makedirs(subFolder)
                if not os.path.exists(checkFile):
                    shutil.move(os.path.join(root, file), subFolder)
                else:
                    global fname, fextension
                    fname, fextension = os.path.splitext(file)
                    ii = 1
                    while True:
                        new_pname = os.path.join(subFolder, fname + "_" + str(ii) + fextension)
                        new_name = fname + "_" + str(ii) + fextension
                        if not os.path.exists(new_pname):
                            shutil.copy(checkFile, new_pname)
                            change_files = "From: " + file + " to: " + new_name + "\n"
                            self.t_start.insert(END, change_files)
                            if self.CheckVar.get() == 1:
                                delfile = os.path.join(root, file)
                                delete_files = "Deleted Original: " + file + "\n"
                                self.t_start.insert(END, delete_files)
                                os.remove(delfile)
                            break
                        ii += 1
            self.progressLine.update_idletasks()
    self.t_start.insert(END, "Process Complete")
    self.progressLine.update_idletasks()

一切正常,我唯一的问题是“stepFiles”变量。我该如何使其比我已经拥有的更准确?

也许这就是我计算stepFiles的方式?我觉得我错过了一些简单的东西。

最佳答案

当你在 Python 中用 2 个 int 进行除法时,你将得到一个 int (整数)。例如,如果您有 27 个文件:

>>> 100 / 27
3

现在我们可以看到这里有一个很大的问题,因为3 * 27 == 81。因此,如果我们对 27 个文件按 3 步前进,那么最终进度条只会达到 81%。

您可以通过将一个 float 输入到您的除法中来解决此问题,以获得更准确的结果:

>>> 100.00 / 27
3.7037037037037037

--

解决此问题的另一种方法是将进度条的最大值设置为self.totalFiles(当然是在计算之后),然后逐步每个文件 1

关于python - Python 2.7 中的 Tkinter ProgressBar 和 Os.Walk,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37752159/

相关文章:

python-2.7 - Scrapy从FTP下载文件

python-2.7 - Tkinter 中的多个窗口 (Python 2.7)

python - Django 中的全局 upload_to 覆盖

python - 试图让 Django 在 VPS 上运行,但我一直遇到 "Invalid option to WSGI daemon process definition"

python - 使用公共(public) GPS 数据计算速度

python - 为什么我的函数落后一键运行?

python - Tkinter 窗口事件 <Visibility>

Python - 无法将用户的所有输入保存到文本文件中

python-2.7 - 如何在 pyspark 中创建具有两个输入的 UDF

google-app-engine - 上传 Google App Engine 应用程序时出错