python - 如何控制进度条大小 PySimpleGUI

标签 python user-interface pysimplegui

我想使用 PySimpleGUI 创建一个进度条,但我希望用户放置进度条的最大值。 这是我的代码:

import PySimpleGUI as sg
import random, time

sg.theme("LightBlue")
progress_value=input()
layout = [[sg.Text("Enter a number out of 50", font='Lucida'),
           sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40))],
          [sg.ProgressBar(progress_value, orientation='h', size=(100, 20), border_width=4, key='-PROGRESS_BAR-',
                          bar_color=("Blue", "Yellow"))],
          [sg.Button('Change Progress'), sg.Exit(),sg.Button('Stop Progress')]]

window = sg.Window("Progress Bar", layout)

while True:
    event, values = window.read()
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break
    progress_value = int(values['-PROGRESS_VALUE-'])
    if event == "Change Progress":
        for i in range(progress_value):
            event, values = window.read(1000)
            if event == "Stop Progress":
                window['-PROGRESS_BAR-'].update(i-1)
                break
            window['-PROGRESS_BAR-'].update(max=progress_value)
            window['-PROGRESS_BAR-'].update(i+1)


window.close()

如您所见,最大值“progress_value”由输入 (progress_value=input()) 给出 但我希望它来自用户的输入文本 (sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40)))该值将设置为progress_value

最佳答案

这是使用单个事件循环执行任务的一种方法

更改 ProgressBar 的最大值时,您也必须设置当前值(在同一更新调用中)。

import PySimpleGUI as sg

sg.theme("LightBlue")
progress_value = 50
layout = [[sg.Text("Enter a number out of 50", font='Lucida'),
           sg.InputText(key='-PROGRESS_VALUE-', font='Lucida, 20', size=(20, 40))],
          [sg.ProgressBar(progress_value, orientation='h', size=(100, 20), border_width=4, key='-PROGRESS_BAR-',
                          bar_color=("Blue", "Yellow"))],
          [sg.Button('Change Progress'), sg.Button('Start Progress'), sg.Button('Stop Progress')]]

window = sg.Window("Progress Bar", layout)
progress_started, counter, timeout = False, 0, None
while True:
    event, values = window.read(timeout=timeout)
    if event == 'Exit' or event == sg.WIN_CLOSED:
        break
    if event == "Change Progress":
        progress_value = int(values['-PROGRESS_VALUE-'])
        # NOTE - must set a current count when changing the max value
        window['-PROGRESS_BAR-'].update(current_count= 0, max=progress_value)
    elif event == 'Start Progress':
        progress_started = True
        counter = 0
        timeout = 1000
    elif event == 'Stop Progress':
        progress_started = False
        timeout = None
    if progress_started:
        window['-PROGRESS_BAR-'].update(counter)
        counter += 1
        if counter > progress_value:
            progress_started = False

window.close()

关于python - 如何控制进度条大小 PySimpleGUI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/73514088/

相关文章:

python - 如何更改 PySimpleGUI 中复选框的大小?

Python tkinter TreeView 列大小

python - 为什么CPLEX的变量选择策略会影响用户分支决策? (Python)

java - 开发用户界面的最佳 Java 框架是什么?

java - try catch java

python - 验证输入值 - PySimpleGUI

python - 如何在 Python 中更新 GUI 窗口?

python - 具有缺失值的列子集的逐行平均值

python - 错误 : command 'gcc' failed with exit status 1 on CentOS

Android:TabSpec setContent(View) 中的 NPE