使用 PySimpleGUI 创建自定义进度条

Anm*_*ena 2 python user-interface tkinter python-3.x pysimplegui

我刚刚开始学习 PySimpleGUI,我想创建一个自定义 GUI 进度条,它将显示程序的哪些部分在不同时间正在运行。

例如,我有一个包含各种组件的视频处理程序,因此我希望进度栏显示如下文本:

“从视频中提取第一帧”

“裁剪图像”

“删除重复图像”

但所有这些行都需要从程序中的不同函数更新进度条窗口,其中与包含进度条的窗口关联的 GUI 代码未运行。

我的代码:

    image_name_list = Frame_Capture(f"{main_dir}\\{input_line2}.mp4")  # Generates image list of frames of video
    Remove_Duplicates(image_name_list)  # Removes duplicate images
    print("Enter name of pdf file")
    pdf_name = f'{input()}.pdf'
    makePdf(pdf_name, image_name_list)  # Converts images to pdf
    Cleanup(image_name_list)  # Deletes unneeded images
    os.startfile(pdf_name)
Run Code Online (Sandbox Code Playgroud)

在这里,当我的程序本身的 GUI 组件在程序的另一部分中运行时,我需要从“Frame_Capture”、“Remove_Duplicates”、“makePDF”和“Cleanup”功能中更新 GUI 进程栏。

我能想到的两种解决方案是:

  1. 全局创建我的窗口并全局更新其中的进度条
  2. 当我的程序到达需要更新进度条的部分时,将所有进度条语句逐行写入文本文件,并同时每隔几毫秒将文本文件中的最新语句加载到我的进度条中。

这些解决方案听起来都不好。我还有其他方法可以做到这一点吗?

len*_*ova 5

为您的 GUI 创建一个主事件循环。当任何功能需要更新 GUI 时,请使用 向主窗口写入一个事件write_event_value(key, value)。例子:

def test():
    import threading
    layout = [[sg.Text('Testing progress bar:')],
              [sg.ProgressBar(max_value=10, orientation='h', size=(20, 20), key='progress_1')]]

    main_window = sg.Window('Test', layout, finalize=True)
    current_value = 1
    main_window['progress_1'].update(current_value)

    threading.Thread(target=another_function,
                     args=(main_window, ),
                     daemon=True).start()

    while True:
        window, event, values = sg.read_all_windows()
        if event == 'Exit':
            break
        if event.startswith('update_'):
            print(f'event: {event}, value: {values[event]}')
            key_to_update = event[len('update_'):]
            window[key_to_update].update(values[event])
            window.refresh()
            continue
        # process any other events ...
    window.close()

def another_function(window):
    import time
    import random
    for i in range(10):
        time.sleep(2)
        current_value = random.randrange(1, 10)
        window.write_event_value('update_progress_1', current_value)
    time.sleep(2)
    window.write_event_value('Exit', '')
Run Code Online (Sandbox Code Playgroud)