如果问题是关于 GUI,那么 PySimpleGUI 允许轻松使用依赖于 Tk、Wx 或 Qt 框架的进度条。这适用于 Linux / Windows。
他们食谱中的例子:
import PySimpleGUI as sg
# layout the window
layout = [[sg.Text('Uploading...')],
[sg.ProgressBar(100, orientation='h', size=(20, 20), key='progressbar')],
[sg.Cancel()]]
# create the window`
window = sg.Window('My Program Upload', layout)
progress_bar = window['progressbar']
# loop that would normally do something useful
for i in range(1000):
# check to see if the cancel button was clicked and exit loop if clicked
event, values = window.read(timeout=10)
if event == 'Cancel' or event == sg.WIN_CLOSED:
break
# update bar with loop value +1 so that bar eventually reaches the maximum
progress_bar.UpdateBar(i + 1)
# TODO: Insert your upload code here
# done with loop... need to destroy the window as it's still open
window.close()
我使用这种进度条并将我的上传功能放在一个单独的线程中,这样 UI 就不会阻塞。