如何在 Kivy 应用程序启动时满足特定条件时自动弹出警报
How to auto-popup the alert when certain condition is met at the start of the Kivy application
我正在寻找一些有条件的 alert() 代码,当应用程序启动时应该 运行 例如如果密码过期,它应该自动弹出警报并在用户单击关闭按钮时关闭应用程序。我已经在 python 和 kivy 中编写了代码。
我曾尝试使用 on_start() 函数,但不知何故无法放置正确的 logic/code.
.py 文件:
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 文件
<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
我不确定我应该使用单独的 .kv 文件进行弹出设计还是继续使用相同的 .kv 文件,在这两种情况下如何 运行 在应用程序启动时自动执行代码.
on_start
函数必须在应用程序的主 class 中定义,然后才能正常工作。如果你打算做一个大型的应用程序,最好使用一个单独的文件.kv
,这样语法也会被高亮显示。
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()
我正在寻找一些有条件的 alert() 代码,当应用程序启动时应该 运行 例如如果密码过期,它应该自动弹出警报并在用户单击关闭按钮时关闭应用程序。我已经在 python 和 kivy 中编写了代码。 我曾尝试使用 on_start() 函数,但不知何故无法放置正确的 logic/code.
.py 文件:
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 文件
<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
我不确定我应该使用单独的 .kv 文件进行弹出设计还是继续使用相同的 .kv 文件,在这两种情况下如何 运行 在应用程序启动时自动执行代码.
on_start
函数必须在应用程序的主 class 中定义,然后才能正常工作。如果你打算做一个大型的应用程序,最好使用一个单独的文件.kv
,这样语法也会被高亮显示。
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()