I am trying to create a simple clock app, and for that, I am using time
. I want to update the time label
Imports
import toga
from toga.style import Pack
from toga.style.pack import COLUMN, ROW
import time
code
self.time_label=toga.Label("00:00:00",style=Pack(padding=10,font_size=50))
I directly call the update_time()
function in the startup method
self.Update_time()
self.time_label
is needed to change to the current time
the text will change inside the Update_time()
function
def Update_time(self):
current_time= time.strftime('%I:%M:%S')
self.time_label.text=current_time
Now, I encounter that it only displays the time and does not change to the current time
For example, if I run the program at 9:22:56 it displays 9:22:56 but when it becomes 9:22:57, or 9:23:00, it still shows 9:22:56.
If you only call the update method once, then of course it will only update once. To update it regularly, you'll need to run an async task.
I think the startup
method is called before the asyncio event loop is running, so instead you could override the on_running
method, like this:
async def on_running(self):
while True:
self.Update_time()
await asyncio.sleep(1)