2 回答

TA貢獻1825條經驗 獲得超4個贊
這奏效了。無需使用檢查線程是否已完成。.after()
import os
from tkinter import *
from tkinter.ttk import *
import threading
def use_pyinstaller(): # this function is to execute pyinstaller command and add value to progressbar.
v = 0
for x in files:
os.system('pyinstaller '+x)
v+=1
p['value'] = v
def begin():
threading.Thread(target=use_pyinstaller).start() # create a non-block thread to start the function.
directory = dir_path = os.path.dirname(os.path.realpath(__file__))
files = os.listdir(directory)
root = Tk()
root.geometry('200x200')
root.maxsize(200,200)
root.minsize(200,200)
root.title('PYTOEXE')
p = Progressbar(root,length=200,max=len(files))
b = Button(root,text="Start",command=begin)
p.place(x=0,y=0)
b.place(x=62,y=30)
root.mainloop()

TA貢獻1891條經驗 獲得超3個贊
首先,按鈕的參數可以只是:。command
command=begin
GUI 工具包(如)是事件驅動的。它們依賴于鍵盤和鼠標事件的順暢流動才能正常工作?;卣{(如來自按鈕)是從事件循環 () 調用的。因此,回調應該只需要很短的時間(例如50毫秒)才能不凍結GUI。因此,切勿在回調中運行長時間運行的循環。你必須以不同的風格編程。tkinter
command
root.mainloop
上面的鏈接將您帶到我網站上的一篇文章,我將簡單的命令行程序與等效的GUI程序進行了比較。雖然該程序不使用外部進程,但它說明了原理。
在 GUI 中執行此操作的正確方法是從按鈕回調開始 。然后使用該方法定期運行檢查 是否已完成的回調,然后啟動新進程。multiprocessing.Process
root.after
Process
添加回答
舉報