Cómo desplazarse automáticamente dentro de una ventana de mensaje de Tkinter
Pregunta
Escribí la siguiente clase para producir "monitoreo" salida dentro de una ventana adicional.
- Desafortunadamente, no se desplaza automáticamente hacia abajo hasta la línea más reciente. ¿Qué está mal?
- Como también tengo problemas con Tkinter e ipython: ¿cómo sería una implementación equivalente con qt4?
Aquí está el código:
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()
Uso:
Monitor.write("Hello World!")
Solución
Agregue una declaración cls.text.see (Tkinter.END)
justo después del inserto que llama.
Otros consejos
Para aquellos que quieran probar el enlace:
def callback():
text.see(END)
text.edit_modified(0)
text.bind('<<Modified>>', callback)
Solo ten cuidado. Como @BryanOakley señaló, el evento virtual modificado solo se llama una vez hasta que se reinicia. Considere a continuación:
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()
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow