I'm trying to write a simple gui-based application in pygtk which provides 'live' previewing of text-based markup. The markup processing, however, can be quite computationally expensive and slow to run, so updating the preview on every keystroke is not really viable. Instead I'd like to have the update run only when user input lapses. Ideally, I'd have it update a specified interval after the last keystroke in a sequence.
I've looked into using threading.Timer
, by cancelling and re-starting a timer to invoke the update function each time the "changed" signal is emitted. I've also tried to use gtk.idle_add()
, but I can't seem to get it working.
Below is a very simple example - could someone suggest the best strategy to achieve what I'm after, preferably with an example?
import gtk
class example:
def __init__(self):
window = gtk.Window()
window.set_title("example")
window.resize(600,400)
box = gtk.HBox(homogeneous = True, spacing = 2)
buf = gtk.TextBuffer()
buf.connect("changed", self.buf_on_change)
textInput = gtk.TextView(buf)
box.add(textInput)
self.lbl = gtk.Label()
box.add(self.lbl)
window.add(box)
window.connect("destroy", gtk.main_quit)
window.show_all()
def buf_on_change(self, buf):
txt = buf.get_text(*buf.get_bounds())
output = self.renderText(txt)
self.lbl.set_text(output)
def renderText(self, txt):
# perform computation-intensive text-manipulation here
output = txt
return output
if __name__ == '__main__':
example()
gtk.main()