2013-07-21 89 views
0

我有一個循環,在蟒蛇像下面讀取文件:Tkinter的進度條

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

如何添加將被鏈接到for循環的Tkinter進度條和對fileList的大小(在循環之前開始並在循環之後關閉)?

THX

回答

2

這個小腳本應該演示如何做到這一點:

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() 

您可以隨時調整它完美地滿足您的需求。