pysimplegui模块实现进度条

一、代码

import PySimpleGUI as sg
layout = [[sg.Text('任务完成进度')],
          [sg.ProgressBar(1000, orientation='h', size=(20, 20), key='progressbar')],
          [sg.Cancel()]]

window = sg.Window('机器人执行进度', layout)
progress_bar = window['progressbar']

for i in range(1000):
    event, values = window.read(timeout=10)
    if event == 'Cancel'  or event is None:
        break
    progress_bar.UpdateBar(i + 1)

window.close()

pysimplegui模块实现进度条_第1张图片

二、进阶应用

import pandas as pd

path = r"*****"
df = pd.read_excel(path)
layout = [[sg.Text('任务完成进度')],
          [sg.Text('', size=(5, 1), font=('Helvetica', 15), justification='center', key='text')],
          [sg.ProgressBar(len(df), orientation='h', size=(50, 20), key='progressbar')],
          [sg.Cancel()]]
window = sg.Window('机器人执行进度', layout)
progress_bar = window['progressbar']
# For循环
for i in range(0, len(df)):
    print(df.iloc[i])
    time.sleep(1) #假设处理的时间
    event, values = window.read(timeout=10)
    if event == 'Cancel' or event is None:
        break
    progress_bar.UpdateBar(i + 1)
    window['text'].update('{}%'.format(int(i / len(df) * 100)))
window.close()

pysimplegui模块实现进度条_第2张图片

你可能感兴趣的:(pysimplegui模块实现进度条)