[pygtk] A Timer and refreshing TextView

John Finlay finlay at moeraki.com
Mon Dec 3 16:29:19 WST 2007


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


More information about the pygtk mailing list