Tkinterメッセージウィンドウ内で自動的にスクロールする方法
質問
「監視」を作成するために次のクラスを作成しました。追加ウィンドウ内の出力。
- 残念ながら、最新の行まで自動的にスクロールしません。何が悪いのですか?
- Tkinterとipythonにも問題があるので、qt4の同等の実装はどのようになりますか?
コードは次のとおりです:
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()
使用法:
Monitor.write("Hello World!")
解決
挿入を呼び出した直後にステートメント cls.text.see(Tkinter.END)
を追加します。
他のヒント
バインドを試してみたいと思う人へ:
def callback():
text.see(END)
text.edit_modified(0)
text.bind('<<Modified>>', callback)
注意してください。 @BryanOakleyが指摘したように、Modified仮想イベントはリセットされるまで一度だけ呼び出されます。以下を考慮してください:
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()
所属していません StackOverflow