2016-04-09 24 views
2

ループ内のTkinterプログレスバーを簡単に更新する方法は?ループ内の進行状況バーを更新するにはどうすればよいですか?

大変混乱することなく解決策が必要なので、私は簡単にスクリプトに実装できます。それはすでにかなり複雑です。

のコードを言ってみましょうです:

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

答えて

2

ここで連続ttkプログレスバーを更新する簡単な例です。あなたはおそらくsleepをGUIに入れたくないでしょう。これは更新が遅くなるため、変更が反映されることがわかります。

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

'root.update_idletasks()を使用すると、ウィンドウが応答しなくなります。 しかし、 'root.update()'はウィンドウを無応答にしません。 – skarfa

関連する問題