简体   繁体   中英

Progress Bar for conditional while loop using PysimpleGUI

I am seeking to add a status bar showing the progress, but not been able to accomplish this, Below is my code. Any help is highly appreciated.

import PySimpleGUI as sg
import pandas as pd
from geopy.geocoders import Nominatim
from geopy.extra.rate_limiter import RateLimiter


sg.theme("DarkTeal2")
layout = [[sg.T("")], [sg.Text("Import file: "), sg.Input(), sg.FileBrowse(key="-IN-")],[sg.Button("Submit")], 
          [sg.Button("Exit")],[sg.ProgressBar(100, orientation= 'h' , size=(50, 10), key= 'progressbar' )]]

###Building Window
window = sg.Window('Geocoder', layout, size=(600,150))
progress_bar = window['progressbar']
while True:
    event, values = window.read()
    if event == "Submit":
        file = values["-IN-"]
        filename = file.split('/')[len(file.split('/'))-1]
        geolocator = Nominatim(user_agent = "geoapiExercises") 
        geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)
        df = pd.read_csv(filename, encoding = 'unicode_escape')
        df['location'] = df['add'].apply(geocode)
        df['Lat'] = df['location'].apply(lambda x: x.latitude if x else None)
        df['Lon'] = df['location'].apply(lambda x: x.longitude if x else None)
        df.to_csv('Geo_test.csv')      
        
    elif event == sg.WIN_CLOSED or event=="Exit":
        break
    for i in range(1,10):
        progress_bar.update(i)
        i=i+=1
window.close()  

So a few things.

First is that you can check out some of the demos on github if you haven't like this one . I am not up-to-date with the PySimpleGUI code, but seems like they are still using .update_bar like progress_bar.update_bar(i) to increment the values. Also I am unsure when exactly you want to update your progress bar, as currently it would not be triggered by any specific event but the while loop. My assumption is that you would like your code to update the progress bar while it executes some task like the lines in the "Submit" event. In that case, you would need to decide how you want to divide the 100 units of progress with any given code. For example:

if event == "Submit":
    progress_bar.update_bar(0, 100)

    file = values["-IN-"]
    filename = file.split('/')[len(file.split('/'))-1]

    progress_bar.update_bar(10, 100)

    geolocator = Nominatim(user_agent = "geoapiExercises") 
    geocode = RateLimiter(geolocator.geocode, min_delay_seconds=1)

    progress_bar.update_bar(30, 100)

    df = pd.read_csv(filename, encoding = 'unicode_escape')
    df['location'] = df['add'].apply(geocode)

    progress_bar.update_bar(60, 100)

    df['Lat'] = df['location'].apply(lambda x: x.latitude if x else None)
    df['Lon'] = df['location'].apply(lambda x: x.longitude if x else None)
    df.to_csv('Geo_test.csv')      

    progress_bar.update_bar(100, 100)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM