PySimpleGUI - ProgressBar Element



Sometimes, a computer operation may be very lengthy, taking a lot of time to complete. Therefore, the user may get impatient. Hence it is important to let him know the state of the application’s progress. The ProgressBar element gives a visual indication of the amount of the process completed so far. It is a vertical or horizontal coloured bar that is incrementally shaded by a contrasting colour to show that the process is in progress.

The ProgressBar constructor has the following parameters, in addition to those common parameters inherited from the Element class −

PySimpleGUI.ProgressBar(max_value, orientation, size, bar_color)

The max_value parameter is required to calibrate the width or height of the bar. Orientation is either horizontal or vertical. The size is (chars long, pixels wide) if horizontal, and (chars high, pixels wide) if vertical. The bar_color is a tuple of two colors that make up a progress bar.

The update() method modifies one or more of the following properties of the ProgressBar object −

  • current_count − sets the current value

  • max − changes the max value

  • bar_color − The two colors that make up a progress bar. First color shows the progress. Second color is the background.

Given below is a simple demonstration of how a ProgressBar control is used. The layout of the window consists of a progress bar and a Test button. When it is clicked, a for loop ranging from 1 to 100 starts

import PySimpleGUI as psg
import time
layout = [
   [psg.ProgressBar(100, orientation='h', expand_x=True, size=(20, 20),  key='-PBAR-'), psg.Button('Test')],
   [psg.Text('', key='-OUT-', enable_events=True, font=('Arial Bold', 16), justification='center', expand_x=True)]
]
window = psg.Window('Progress Bar', layout, size=(715, 150))
while True:
   event, values = window.read()
   print(event, values)
   if event == 'Test':
      window['Test'].update(disabled=True)
      for i in range(100):
         window['-PBAR-'].update(current_count=i + 1)
         window['-OUT-'].update(str(i + 1))
         time.sleep(1)
         window['Test'].update(disabled=False)
   if event == 'Cancel':
      window['-PBAR-'].update(max=100)
   if event == psg.WIN_CLOSED or event == 'Exit':
      break
window.close()

It will produce the following output window −

Progress Bar
pysimplegui_element_class.htm
Advertisements