Comment faire défiler automatiquement dans une fenêtre de message Tkinter
Question
J'ai écrit la classe suivante pour la production de "surveillance". sortie dans une fenêtre supplémentaire.
- Malheureusement, il ne défile pas automatiquement jusqu'à la dernière ligne. Quel est le problème?
- Comme j'ai aussi des problèmes avec Tkinter et ipython: à quoi ressemblerait une implémentation équivalente avec qt4?
Voici le 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()
Utilisation:
Monitor.write("Hello World!")
La solution
Ajouter une instruction cls.text.see (Tkinter.END)
juste après l'insertion d'un appelant.
Autres conseils
À ceux qui voudraient essayer de se lier:
def callback():
text.see(END)
text.edit_modified(0)
text.bind('<<Modified>>', callback)
Fais juste attention. Comme @BryanOakley l'a souligné, l'événement virtuel modifié n'est appelé qu'une fois jusqu'à sa réinitialisation. Considérez ci-dessous:
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()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow