[英]Progress Bar for conditional while loop using PysimpleGUI
我正在寻求添加一个显示进度的状态栏,但无法做到这一点,下面是我的代码。 非常感谢任何帮助。
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()
所以有几件事。
首先,如果您不喜欢这个,可以查看 github 上的一些演示。 我不是最新的 PySimpleGUI 代码,但似乎他们仍在使用.update_bar
像progress_bar.update_bar(i)
来增加值。 此外,我不确定您何时想要更新进度条,因为目前它不会由任何特定事件触发,而是由while
循环触发。 我的假设是您希望您的代码在执行某些任务时更新进度条,例如"Submit"
事件中的行。 在这种情况下,您需要决定如何将 100 个单位的进度与任何给定的代码分开。 例如:
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.