如何解决计划小部件更新Python
我目前正在尝试在用KivyMD制作的应用程序中制作一个时钟小部件。我在Screen on_pre_enter事件上获得了预期的结果。但是,每分钟过后,我都无法更新时钟值。我正在尝试使用schedule module进行更新。
我当前的代码如下:
Python代码:
class MainMenu(Screen):
def date_time(self):
now = datetime.datetime.now()
dateStr = now.strftime("%d/%m/%Y")
self.ids.date.text = dateStr
hourStr = now.strftime("%I:%M")
self.ids.hour.text = hourStr
am_pm_Str = now.strftime("%p")
self.ids.am_pm.text = am_pm_Str
def update_date_time(self):
schedule.every().minute.at(":01").do(self.date_time)
KV代码:
<MainMenu>:
name: "main"
on_pre_enter: root.date_time()
# on_enter: root.update_date_time() # where do I have to call this function?
FloatLayout:
cols:1
size: root.size
pos: root.pos
canvas.before:
Rectangle:
pos: self.pos
size: self.size
source: 'mainbckd.png'
FloatLayout:
size: root.width,root.height/2
MDCard:
size_hint: (0.20,0.20)
pos_hint: {'x': 0.025,'top': 0.93}
orientation: 'vertical'
padding: '10dp'
spacing: '10dp'
halign: 'center'
md_bg_color: [0.95,0.95,1]
opacity: 0.40
MDCard:
orientation: 'horizontal'
md_bg_color: [0.95,1]
opacity: 1
MDLabel:
id: date
halign: 'center'
text: 'Date'
bold: True
opacity: 1
font_size: (root.width**2 + root.height**2) / 15**4
MDCard:
orientation: 'horizontal'
md_bg_color: [0.95,1]
opacity: 1
MDLabel:
id: hour
bold: True
halign: 'center'
font_size: (root.width**2 + root.height**2) / 15.5**4
MDLabel:
id:am_pm
halign: 'center'
opacity: 1
font_size: (root.width**2 + root.height**2) / 15.5**4
我不知道何时或何处运行update_date_time函数以成功实现运行良好的时钟。 任何帮助将不胜感激。
非常感谢。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。