我有一个循环读取python中的文件,如下所示:
def Rfile():
for fileName in fileList:
….
如何添加将链接到 for 循环和 fileList 大小的 tkinter 进度条(在循环之前开始并在循环之后关闭)?
谢谢
我有一个循环读取python中的文件,如下所示:
def Rfile():
for fileName in fileList:
….
如何添加将链接到 for 循环和 fileList 大小的 tkinter 进度条(在循环之前开始并在循环之后关闭)?
谢谢
这个小脚本应该演示如何做到这一点:
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()
您可以随时调整它以完全满足您的需求。