2016-04-09 8 views
2

Was ist die einfache Methode, Tkinter Fortschrittsbalken in einer Schleife zu aktualisieren?Wie aktualisiert man einen Fortschrittsbalken in einer Schleife?

Ich brauche eine Lösung ohne viel Unordnung, so kann ich es leicht in mein Skript implementieren, da es schon ziemlich kompliziert für mich ist.

Sagen wir mal der Code:

from Tkinter import * 
import ttk 


root = Tk() 
root.geometry('{}x{}'.format(400, 100)) 
theLabel = Label(root, text="Sample text to show") 
theLabel.pack() 


status = Label(root, text="Status bar:", bd=1, relief=SUNKEN, anchor=W) 
status.pack(side=BOTTOM, fill=X) 

root.mainloop() 

def loop_function(): 
    k = 1 
    while k<30: 
    ### some work to be done 
    k = k + 1 
    ### here should be progress bar update on the end of the loop 
    ### "Progress: current value of k =" + str(k) 


# Begining of a program 
loop_function() 

Antwort

2

Hier ist ein kurzes Beispiel kontinuierlich eine ttk progressbar aktualisieren. Wahrscheinlich möchten Sie sleep nicht in eine GUI einfügen. Dies ist nur um die Aktualisierung zu verlangsamen, so dass Sie sehen, wie es sich ändert.

from Tkinter import * 
import ttk 
import time 

MAX = 30 

root = Tk() 
root.geometry('{}x{}'.format(400, 100)) 
progress_var = DoubleVar() #here you have ints but when calc. %'s usually floats 
theLabel = Label(root, text="Sample text to show") 
theLabel.pack() 
progressbar = ttk.Progressbar(root, variable=progress_var, maximum=MAX) 
progressbar.pack(fill=X, expand=1) 


def loop_function(): 

    k = 0 
    while k <= MAX: 
    ### some work to be done 
     progress_var.set(k) 
     k += 1 
     time.sleep(0.02) 
     root.update_idletasks() 
    root.after(100, loop_function) 

loop_function() 
root.mainloop() 
+1

Mit 'root.update_idletasks()' werden die Fenster nicht mehr reagiert. Aber 'root.update()' lässt das Fenster nicht reagieren. – skarfa

Verwandte Themen