Como rolar automaticamente dentro de uma janela de mensagem Tkinter
Pergunta
Eu escrevi a seguinte classe para a produção de "monitorização" saída dentro de uma janela extra.
- Infelizmente não rola automaticamente até a linha mais recente. O que está errado?
- Como eu também tenho problemas com Tkinter e ipython: como é que uma implementação equivalente com olhar qt4 como
Aqui está o 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!")
Solução
Adicionar um direito declaração cls.text.see(Tkinter.END)
após a inserção chamando.
Outras dicas
Para aqueles que pode querer tentar de ligação:
def callback():
text.see(END)
text.edit_modified(0)
text.bind('<<Modified>>', callback)
Basta ter cuidado. Como @BryanOakley apontou, o evento virtual modificados só é chamado uma vez até que seja reiniciado. Considere abaixo:
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 em: CC-BY-SA com atribuição
Não afiliado a StackOverflow