How to automatically scroll the Tkinter message box

I wrote the following class to create "monitoring" output in an additional window.

  • Unfortunately, it does not scroll automatically to the very last line. What's wrong?
  • Since I also have problems with Tkinter and ipython: what would the equivalent implementation look like with qt4?

Here is the code:

import Tkinter class Monitor(object): @classmethod def write(cls, s): try: cls.text.insert(Tkinter.END, str(s) + "\n") cls.text.update() except Tkinter.TclError, e: print str(s) mw = Tkinter.Tk() mw.title("Message Window by my Software") text = Tkinter.Text(mw, width = 80, height = 10) text.pack() 

Using:

 Monitor.write("Hello World!") 
+7
python qt qt4 ipython tkinter
source share
2 answers

Add cls.text.see(Tkinter.END) immediately after one call insertion.

+26
source share

For those who want to try to bind:

 def callback(): text.see(END) text.edit_modified(0) text.bind('<<Modified>>', callback) 

Just be careful. As @BryanOakley noted, a modified virtual event is fired only once until reset. Consider below:

 import Tkinter as tk def showEnd(event): text.see(tk.END) text.edit_modified(0) #IMPORTANT - or <<Modified>> will not be called later. if __name__ == '__main__': root= tk.Tk() text=tk.Text(root, wrap=tk.WORD, height=5) text.insert(tk.END, "Can\nThis\nShow\nThe\nEnd\nor\nam\nI\nmissing\nsomething") text.edit_modified(0) #IMPORTANT - or <<Modified>> will not be called later. text.pack() text.bind('<<Modified>>',showEnd) button=tk.Button(text='Show End',command = lambda : text.see(tk.END)) button.pack() root.mainloop() 
+3
source share

All Articles