2016-07-25 65 views
1

運行tkinter的函數會凍結。 我想使用運行該進程的Tkinter窗口。在運行進度條 我想使用tkinter window.but但我不能 ,因爲它凍結tkinter。 如何使用根窗口,而time.sleep(10)或其他功能工作如何在tkinter中使用作品

import tkinter.ttk as ttk 
import tkinter as tk 
import time 

progress = 0 


def loading(window=None): 
    mpb = ttk.Progressbar(window, orient="horizontal", length=200, mode="determinate") 
    mpb.place(y=0, x=0) 
    mpb["maximum"] = 100 
    mpb["value"] = progress 
    print(progress) 


def incrase(): 
    global progress 
    print(progress) 
    progress += 1 
    time.sleep(10) # for example, a function works here and tkinter freezes 
    loading()  # i don't want tkinter freezes 


root = tk.Tk() 
loading(root) 
ttk.Button(root, text='increase', command=incrase).place(x=0, y=25, width=90) 

root.mainloop() 

感謝答案

回答

1

您應該使用after()與您可以安排loading()功能後,一些被稱爲一段的時間。

計劃

這裏是你可以在你的程序中使用它:

import tkinter.ttk as ttk 
import tkinter as tk 
import time 

progress = 0 


def loading(window): 
    mpb = ttk.Progressbar(window, orient="horizontal", length=200, mode="determinate") 
    mpb.place(y=0, x=0) 
    mpb["maximum"] = 100 
    mpb["value"] = progress 
    print(progress) 


def incrase(): 
    global root 
    global progress 
    print(progress) 
    progress += 1 
    root.after(10, loading(root)) # schedule loading() 



root = tk.Tk() 
loading(root) 
ttk.Button(root, text='increase', command=incrase).place(x=0, y=25, width=90) 

root.mainloop() 

演示

截圖上面正在運行的程序:

enter image description here

相關問題