python多執行緒與進度條的結合
阿新 • • 發佈:2019-01-27
我把其中最關鍵的部分通過一個簡單的例子來說明:
import threading import tkinter as tk from time import ctime,sleep # 建立主視窗 window = tk.Tk() window.title('測試') window.geometry('630x200') def music(): for i in range(2): print("I was listening to music %s" % ctime()) sleep(1) def move(): for i in range(2): print("I was at the movie %s" % ctime()) sleep(1) def test(): # 多執行緒 threads = [] t1 = threading.Thread(target=music) threads.append(t1) t2 = threading.Thread(target=move) threads.append(t2) for t in threads: t.setDaemon(True) t.start() btn_download = tk.Button(window, text='啟動', command=test) btn_download.place(x=400, y=150) window.mainloop()
請看下面列印的執行結果,觀察每個結果後面對應的時間,多執行緒的精華之處就在此。
結合提供的原始碼,學會怎樣實現多執行緒。
tkinter中多執行緒與進度條的結合可以參考我的另一篇部落格: