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가 지적했듯이 수정 된 가상 이벤트는 재설정 될 때까지 한 번만 호출됩니다. 아래를 고려하십시오 :
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