Reputation: 23
I am looking for some conditional alert() code which should run when the app starts e.g. if password is expired than it should auto popup the alert and close the app when user clicks on close button. I have written the code in python and kivy. I have tried using the on_start() function but somehow not able to place a proper logic/code.
.py file:
def closeapp(self, arg):
App.get_running_app().stop()
Window.close()
def on_start(self):
exp_date = "24/05/2021"
past = datetime.strptime(exp_date, "%d/%m/%Y")
present = datetime.now()
if past.date() <= present.date():
print (" Password expired. ")
self.popup.open()
else:
pass
class MyLayout(TabbedPanel):
## Main code of the app is written in this section
class MyApp(App):
def build(self):
on_start(self)
return MyLayout()
.kv file
<MyLayout>
do_default_tab: False
size_hint: 1, 1
padding: 10
tab_pos: 'top_left'
TabbedPanelItem:
id: tab1
##Afer this Main design code starts except the Popup code
I am not sure whether I should use separate .kv files for popup design or continue to use the same .kv file, in both the scenarios how to run the code automatically at the start of the app.
Upvotes: 0
Views: 527
Reputation: 1397
The on_start
function must be defined in the main class of the application, then it works correctly. If you plan to make a large application, it is better to use a separate file .kv
, so the syntax will also be highlighted.
from kivy.app import App
from kivy.uix.tabbedpanel import TabbedPanel
from kivy.uix.popup import Popup
from kivy.lang import Builder
from datetime import datetime
Builder.load_string("""
<MyLayout>
do_default_tab: False
size_hint: 1, 1
padding: 10
tab_pos: 'top_left'
TabbedPanelItem:
id: tab1
<MyPopup>:
title: 'Alert'
auto_dismiss: False
size_hint: None, None
size: 400, 400
BoxLayout:
orientation: 'vertical'
Label:
text: 'Text'
Button:
text: 'Close me!'
on_release:
root.dismiss()
app.close_app()
""")
class MyPopup(Popup):
pass
class MyLayout(TabbedPanel):
pass
class MyApp(App):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.popup = MyPopup()
def build(self):
return MyLayout()
@staticmethod
def close_app(*args):
App.get_running_app().stop()
def on_start(self):
exp_date = "24/05/2021"
past = datetime.strptime(exp_date, "%d/%m/%Y")
present = datetime.now()
if past.date() <= present.date():
print(" Password expired. ")
self.popup.open()
MyApp().run()
Upvotes: 0