views:

120

answers:

2

I am writing a timer program in Python using PyGTK. It is precise to the hundredths place. Right now, I am using a constantly updated label. This is a problem, because if I resize the window while the timer is running, Pango more often than not throws some crazy error and my program terminates. It's not always the same error, but different ones that I assume are some form of failed draw. Also, the label updates slower and slower as I increase the font size.

So, I am wondering if there is a more correct way to display the timer. Is there a more stable method than constantly updating a label?

+1  A: 

Updating a label should work perfectly reliably, so I suspect you're doing something else wrong. Are you using threads? What does your code look like? How small can you condense your program (by removing functionality, not by obfuscating the code), without making the problem go away?

Lars Wirzenius
Yes, the label is updated from a thread. I'm not sure what you are getting at with the second question, but I can take a lot of things out of the program while preserving the problem. The timer is actually only a part of the program, but the problem occurred even when there were no other features present.
linkmaster03
I am always suspicious of threads and gtk: it's possible, but hard, to get it right. Your symptoms do sound like there is a missing gtk threads_enter/threads_leave pair missing somewhere.
Lars Wirzenius
My other question was motivated by the fact that the process of minimizing a program, while preserving the problem, often leads to it becoming easier to see what the problem is.
Lars Wirzenius
This is probably the same problem as http://stackoverflow.com/questions/2066767/gui-not-updated-from-another-thread-when-using-pygtk
Johannes Sasongko
A: 

I figured out the problem. It was indeed a problem with the threads. I never would've guessed that myself. The trick is to use gobject.timeout_add() to create a timer instead of a threaded loop. Here is some information about gobject.timeout_add():

http://nkour.blogspot.com/2005/06/simple-timer-in-pygtk.html

http://faq.pygtk.org/index.py?req=show&file=faq01.021.htp

Don't forget to have your function return True, or the timer will stop.

linkmaster03