[pygtk] Re: A Timer and refreshing TextView
Mark Stahler
isignupnow at gmail.com
Mon Dec 3 23:18:40 WST 2007
John Finlay wrote:
> Nathaniel Smith wrote:
>> On Sun, Dec 02, 2007 at 09:34:40PM -0800, Aravind Vijayakumar wrote:
>>
>>> Since you are opening the file afresh each time in refreshLog, won't
>>> you be reading the first line each time?
>>
>> The .read() method in Python reads the whole file, not just the first
>> line.
>>
>>
> read() will also read from the current position so you can keep reading
> as new data is added. An alternative is to not open the file in
> refreshLog but when initializing and then read it in refreshLog which is
> called by timeout_add - something like:
>
> Code:
>
> textview = self.gui.get_widget("bottom_textview")
> # Methods that need to be run on start
> file = open('log.txt')
> self.refreshLog()
> # Timer to autorefresh log
> timer = gobject.timeout_add(500, self.refreshLog)
> timer.start()
>
> def refreshLog(self):
> string = file.read()
> if string:
> buffer = textview.get_buffer()
> mark = buffer.create_mark("end", buffer.get_end_iter())
> textview.scroll_to_mark(mark, 0)
> buffer.insert_at_cursor(string)
> buffer.delete_mark_by_name("end")
> return True
>
> John
> _______________________________________________
> pygtk mailing list pygtk at daa.com.au
> http://www.daa.com.au/mailman/listinfo/pygtk
> Read the PyGTK FAQ: http://www.async.com.br/faq/pygtk/
>
Thank you all for the replies. I was not aware of the problems with the
threading in GTK+.
I got it working perfectly using the gobject timeout with Johns modified
routine. I now only have to insert new text from the log and dont have
to reread the whole thing.
Thanks!
More information about the pygtk
mailing list