简体   繁体   English

使用 PysimpleGUI 进行条件 while 循环的进度条

[英]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 .首先,如果您不喜欢这个,可以查看 github 上一些演示。 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.我不是最新的 PySimpleGUI 代码,但似乎他们仍在使用.update_barprogress_bar.update_bar(i)来增加值。 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.此外,我不确定您何时想要更新进度条,因为目前它不会由任何特定事件触发,而是由while循环触发。 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.我的假设是您希望您的代码在执行某些任务时更新进度条,例如"Submit"事件中的行。 In that case, you would need to decide how you want to divide the 100 units of progress with any given code.在这种情况下,您需要决定如何将 100 个单位的进度与任何给定的代码分开。 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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