Countdown timer with wx.StaticText

Hai all…
I want to implement the following program in wx.StaticText on wxpython.

timer.py (1.04 KB)

···

from datetime import timedelta
from time import sleep
hours, minutes, seconds = 0, 2, 10
total = timedelta(hours=hours, minutes=minutes, seconds=seconds)
for passed_seconds in range(int(total.total_seconds())):
print 'Time Remaining ', total - timedelta(seconds=passed_seconds)
sleep(1)

I tried my best to implement it. My program is attached. But, often it exits with the following error.

**
Pango:ERROR:/build/buildd/pango1.0-1.28.4/./pango/pango-layout.c:3743:pango_layout_check_lines: assertion failed: (!layout->log_attrs)
Aborted


Any idea ?

Navaneeth Suresh wrote:

Hai all...

I want to implement the following program in wx.StaticText on wxpython.

------------------------------------------------------------------------------------------------------------------------

from datetime import timedelta
from time import sleep

hours, minutes, seconds = 0, 2, 10
total = timedelta(hours=hours, minutes=minutes, seconds=seconds)
*for* passed_seconds *in* range(int(total.total_seconds())):
*print* 'Time Remaining ', total - timedelta(seconds=passed_seconds)
sleep(1)

-----------------------------------------------------------------------------------------------------------------------------

I tried my best to implement it. My program is attached. But, often it
exits with the following error.

First, you shouldn't be calling self.SetLabel from the worker thread. All interactions with GUI objects (create modify, etc.) should be done only from the GUI thread.

Second, you can do what you want much more simply by using a wx.Timer instead of a thread. Just create a timer:

     self.timer = wx.Timer(self)

bind an event handler for EVT_TIMER:

     self.Bind(wx.EVT_TIMER, self.UpdateEvent, self.timer)

and start the timer:

    self.timer.Start(1000)

In the UpdateEvent method just calculate the new value to display, call self.SetLabel, and return (so instead of a loop you have just one iteration of the loop.) When the countdown is finished, just call:

     self.timer.Stop()

···

--
Robin Dunn
Software Craftsman