Reputation: 109
I am looking for a way to use a PYSimpleGUI progress bar... without a loop I have looked for several days on the internet with no luck to find an example.
seems everybody does their example with a loop, or on a timer.
I'd like to do something more like a definition that I Can call to to update
I dont know what to change to make it a manually updated item... I want to be able to tell it i=0 at teh beginning of my script and periodically place update marks thru the script(i=i+4) so that I can update it as each Major Step in my script is done
This is the PySimpleGUI script, plus some lines showing what I want to do This currently auto iterates...and I do not know how to change it
I'm just trying to learn and cant find any examples online to do what I want to do.
import PySimpleGUI as sg
import time
from time import sleep
import PySimpleGUI as sg
def prog():
layout = [[sg.Text('Completed Tasks')],
[sg.ProgressBar(100, orientation='h', size=(50, 20), key='progressbar')],
[sg.Cancel()]]
window = sg.Window('Progress').Layout(layout)
progress_bar = window.FindElement('progressbar')
for i in range(100):
event, values = window.Read(timeout=0)
progress_bar.UpdateBar(i + 4)
time.sleep(2)
window.Close()
prog()
time.sleep(2)
#______________________________________________________________
#I'd like to be able to do this
#i=0 at this point
prog()
#do Scripty Stuff
#Update Progress Bar Manually
#i=4 at this point
#do more scriptic writings
#Update Progress bar Manually
#i=8 at this point
#and so forth and so on until I reach 100
Upvotes: 3
Views: 6146
Reputation: 109
Just a quick note for everyone that is finding this answer in 2021 or later:
The window.FindElement()
is deprecated, one uses window.find_element()
nowadays :)
@soundtechscott: Thanks for your answer, I also encountered that issue today and your solution worked out for me.
Upvotes: 2
Reputation: 109
Figured it out
just leave everything as a single line, not a definition
heres an example to help others
I just did real numbers in the Update bar section but you can use variables (i=0) and then update it in the script with an i=i+1 and then use i as your number in the update bar function
i=0
progress_bar.UpdateBar(i, 5)
#i returns a value of 0
i=i+1
progress_bar.UpdateBar(i, 5)
#i now returns a vlaue of 1
#repeat until you reach your maximum value
#this Script will create a Progress Bar
#The Progress will be Manually Updated using the format listed below
#progress_bar.UpdateBar(Value of Bar, Maximum Bar Value)
#the Window uses a .Finalize Function to make the window Persistent
#Import the PySimpleGUI Library
import PySimpleGUI as sg
#Import the Time Library for use in this script
import time
#this is for the Layout Design of the Window
layout = [[sg.Text('Custom Text')],
[sg.ProgressBar(1, orientation='h', size=(20, 20), key='progress')],
]
#This Creates the Physical Window
window = sg.Window('Window Title', layout).Finalize()
progress_bar = window.FindElement('progress')
#This Updates the Window
#progress_bar.UpdateBar(Current Value to show, Maximum Value to show)
progress_bar.UpdateBar(0, 5)
#adding time.sleep(length in Seconds) has been used to Simulate adding your script in between Bar Updates
time.sleep(.5)
progress_bar.UpdateBar(1, 5)
time.sleep(.5)
progress_bar.UpdateBar(2, 5)
time.sleep(.5)
progress_bar.UpdateBar(3, 5)
time.sleep(.5)
progress_bar.UpdateBar(4, 5)
time.sleep(.5)
progress_bar.UpdateBar(5, 5)
time.sleep(.5)
#I paused for 3 seconds at the end to give you time to see it has completed before closing the window
time.sleep(3)
#This will Close The Window
window.Close()
Upvotes: 4