Pmw.ScrolledText programatically scrolling

Graham Dumpleton grahamd at dscpl.com.au
Sun Sep 15 09:06:36 CEST 2002


Joe Connellan <joec at mill.co.uk> wrote in message news:<3D81D44E.C5FC4A8B at mill.co.uk>...
> I've got a Pmw.ScrolledText() that I'm using as a log and I've written
> an addToLog() method like so:
> 
>  def addToLog(self, logString):
>   self.logScr.configure(text_state = 'normal')
>   self.logScr.insert(END, logString)
>   self.logScr.configure(text_state = 'disabled')
> 
> I now want it to scroll when I add a line so the user doesn't have to
> constantly scroll down to view the most recent lines.
> 
> I can't find any info on doing this with ScrolledText or its text
> component - do I need to reposition the scrollbars for it to scroll?

Another slightly different way to what others have suggested is the
following:

  def __init__(self,...):
    ...
    self._eventView = Pmw.ScrolledText(g1.interior(),text_padx=4,
        text_pady=4,text_wrap="char",text_state="disabled",
        vscrollmode="static")
    self._eventView.pack(side="top",padx=8,pady=8,fill="both",expand="true")
    self._eventView.yview("moveto",1.0)

  def _clearLog(self):
    self._eventView.configure(text_state="normal")
    self._eventView.clear()
    self._eventView.configure(text_state="disabled")
    self._eventView.yview("moveto",1.0)
 
  def _appendLog(self,message):
    p1,p2 = self._eventView.yview()
    self._eventView.configure(text_state="normal")
    self._eventView.insert("end",message)
    self._eventView.configure(text_state="disabled")
    if (p1 == 0.0 and p2 == 0.0) or p2 == 1.0:
      self._eventView.yview("moveto",1.0)

A trick in this code is that one uses yview() to determine what of the
text is being shown and will only move to last text inserted if the scroll
area was already at the end.

The reason for this is that it can be mighty annoying to have the scrolled
area always go to the end if you have used the scroll bar to explicitly
go to an earlier part of the text to look at it.



More information about the Python-list mailing list