簡體   English   中英

如何控制進度條大小 PySimpleGUI

[英]How to control Progress bar size 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”的最大值由input (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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM