How Can I Show "please Wait Gif" Image Before The Process Is Complete
Solution 1:
Since you are triggering the build()
method by using the on_enter
attribute, you can accomplish what you want by using that method.
First, you are calling:
super(ListApp, self).__init__(**kwargs)
from the build()
method. You should not call that super
method except from within an __init__()
method override. So that line should be removed. Since you have not written an __init__()
method for ListApp
, there is no need to call the super class __init__()
.
I suggest renaming your build()
method to actual_build()
like this:
defactual_build(self, *args):
and define a new build()
method as:
defbuild(self, **kwargs):
self.popup = Popup(title='Preparing ListApp', content=Image(source='please_wait.gif', anim_delay=0.05))
self.popup.open()
threading.Thread(target=self.actual_build).start()
The above method displays the animated gif, and starts the actual_build()
method (formerly named build()
).
Then, modify the actual_build()
method as:
defactual_build(self, *args):
# super(ListApp, self).__init__(**kwargs)
flag = True
screen = self
# if flag:### sm.add_widget(ListApp(name='Stoploss_ip'))
end = datetime(2020, 12, 14)
start = datetime(2020, 12, 14)
btn = Button(text="Back",
font_size="20sp",
background_color=(255/255, 229/255, 204/255, 1),
color=(1, 1, 1, 1),
size=(12, 12),
size_hint=(.1, .05),
pos=(600, 500))
btn.bind(on_press=lambda *args: setattr(sm, 'current', "option_screen"))
scroll = ScrollView()
list_view = MDList()
scroll.add_widget(list_view)
i = 0
fl = len(file.index)
try:
for index inrange(fl):
for index inrange(1):
columnSeriesObj2 = file.iloc[:, 0]
df = web.DataReader(columnSeriesObj2.values[i],'yahoo', start, end,retry_count=3)
print(df.head())
Objname = file.iloc[:, 2]
columnSeriesObj = df.iloc[:, 3]
columnSeriesObj1 = file.iloc[:, 1]
ObjStoploss = file.iloc[:, 3]
cp = iter(columnSeriesObj.values)
pp = iter(columnSeriesObj1.values)
pp1 = next(pp)
cp1 = columnSeriesObj.values[0]
sl = columnSeriesObj1.values[i] - (columnSeriesObj1.values[i] * (ObjStoploss.values[i]/100))
if cp1 <= sl:
image = ImageLeftWidget(source='loss.png')
items = ThreeLineAvatarIconListItem(text="Alert sale " + Objname.values[i], secondary_text='Close price: '+str(cp1),
tertiary_text='Stoploss: ' + str(sl))
items.add_widget(image)
list_view.add_widget(items)
i=i+1else:
image = ImageLeftWidget(source='profit.jpg')
items = ThreeLineAvatarIconListItem(text="Chill " + Objname.values[i],
secondary_text='Close price: ' + str(cp1),
tertiary_text='Stoploss: ' + str(sl))
items.add_widget(image)
list_view.add_widget(items)
i=i+1except ConnectionAbortedError:
print("Check your Internet connection")
except ConnectionRefusedError:
print("Check your Internet connection")
except ConnectionError:
print("Check your Internet connection")
except ConnectionResetError:
print("Check your Internet connection")
except TimeoutError:
print("Timeout!!!!...Check your Internet connection")
except KeyError:
passexcept:
print("Something went wrong")
# flag = False# if flag ==False:# screen.add_widget(scroll)# screen.add_widget(btn)# schedule the code that must be run on the main thread
Clock.schedule_once(partial(self.finish_build, scroll, btn))
# dismiss the animated gif
self.popup.dismiss()
The above actual_build()
method does everything that the original build()
method did, except for the actual changes to the GUI (that must be done on the main thread). At the end of this method, a call to finish_build()
is scheduled, and the animated gif Popup
is dismissed.
Finally, add a finish_build()
method that does the actual GUI changes:
deffinish_build(self, scroll, btn, dt):
screen = self
screen.add_widget(scroll)
screen.add_widget(btn)
Solution 2:
Now that I can actually run your code, I have an updated answer. First, some of the ListApp
screen can be constructed in the kv
file as:
<ListApp>:name:'Stoploss_ip'on_enter:root.build()ScrollView:MDList:id:list_viewButton:text:"Back"font_size:"20sp"background_color:(255/255,229/255,204/255,1)color:(1,1,1,1)size:(12,12)size_hint:(.1,.05)pos:(600,500)on_press:root.manager.current="option_screen"
Then the ListApp
class can be:
classListApp(Screen):
built = BooleanProperty(False)
defbuild(self):
if self.built:
return
self.built = True
self.popup = Popup(title='Calculating Stoploss', content=Image(source='please_wait.gif'))
self.popup.open()
threading.Thread(target=self.actual_build).start()
defactual_build(self):
end = datetime.today().date()
start = end
i = 0
fl = len(file.index)
try:
for index inrange(fl):
for index inrange(1):
columnSeriesObj2 = file.iloc[:, 0]
df = web.DataReader(columnSeriesObj2.values[i],'yahoo', start, end,retry_count=3)
print(df.head())
Objname = file.iloc[:, 2]
columnSeriesObj = df.iloc[:, 3]
columnSeriesObj1 = file.iloc[:, 1]
ObjStoploss = file.iloc[:, 3]
cp = iter(columnSeriesObj.values)
pp = iter(columnSeriesObj1.values)
pp1 = next(pp)
cp1 = columnSeriesObj.values[0]
sl = columnSeriesObj1.values[i] - (columnSeriesObj1.values[i] * (ObjStoploss.values[i]/100))
if cp1 <= sl:
Clock.schedule_once(partial(self.add_loss, Objname.values[i], str(cp1), str(sl)))
i=i+1else:
Clock.schedule_once(partial(self.add_profit, Objname.values[i], str(cp1), str(sl)))
i=i+1except ConnectionAbortedError:
print("Check your Internet connection")
except ConnectionRefusedError:
print("Check your Internet connection")
except ConnectionError:
print("Check your Internet connection")
except ConnectionResetError:
print("Check your Internet connection")
except TimeoutError:
print("Timeout!!!!...Check your Internet connection")
except KeyError:
passexcept:
pass# print("Something went wrong")print("Done")
self.popup.dismiss()
defadd_loss(self, name, close_price, stop_loss, dt):
image = ImageLeftWidget(source='loss.png')
items = ThreeLineAvatarIconListItem(text="Alert sale " + name, secondary_text='Close price: '+close_price,
tertiary_text='Stoploss: ' + stop_loss)
items.add_widget(image)
self.ids.list_view.add_widget(items)
defadd_profit(self, name, close_price, stop_loss, dt):
image = ImageLeftWidget(source='profit.jpg')
items = ThreeLineAvatarIconListItem(text="Chill " + name,
secondary_text='Close price: ' + close_price,
tertiary_text='Stoploss: ' + stop_loss)
items.add_widget(image)
self.ids.list_view.add_widget(items)
The main items changed are that the list and the back button are built in the 'kv', and the items for the list are built using Clock.schedule_once()
. There is no longer any need for the finish_build()
method.
Also, note that I have added a built
attribute that keeps track of whether the screen has been built, so that the screen doesn't get built multiple times. If it was built multiple times, the items in the list would get added repeatedly.
Post a Comment for "How Can I Show "please Wait Gif" Image Before The Process Is Complete"