【问题标题】:tkinter progress bar with file list带有文件列表的 tkinter 进度条
【发布时间】:2013-07-21 21:21:09
【问题描述】:

我有一个循环读取 python 中的文件,如下所示:

def Rfile():
    for fileName in fileList:
….

如何添加将链接到 for 循环和 fileList 大小的 tkinter 进度条(在循环之前开始并在循环之后关闭)?。

谢谢

【问题讨论】:

    标签: python-3.x tkinter progress-bar


    【解决方案1】:

    这个小脚本应该演示如何做到这一点:

    import tkinter as tk
    from time import sleep
    
    # The truncation will make the progressbar more accurate
    # Note however that no progressbar is perfect
    from math import trunc
    
    # You will need the ttk module for this
    from tkinter import ttk
    
    # Just to demonstrate
    fileList = range(10)
    
    # How much to increase by with each iteration
    # This formula is in proportion to the length of the progressbar
    step = trunc(100/len(fileList))
    
    def MAIN():
        """Put your loop in here"""
        for fileName in fileList:
            # The sleeping represents a time consuming process
            # such as reading a file.
            sleep(1)
    
            # Just to demonstrate
            print(fileName)
    
            # Update the progressbar
            progress.step(step)
            progress.update()
    
        root.destroy()
    
    root = tk.Tk()
    
    progress = ttk.Progressbar(root, length=100)
    progress.pack()
    
    # Launch the loop once the window is loaded
    progress.after(1, MAIN)
    
    root.mainloop()
    

    您可以随时调整它以完美满足您的需求。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-15
      • 2013-07-30
      • 1970-01-01
      • 1970-01-01
      • 2018-04-04
      相关资源
      最近更新 更多