14

我编写了以下类,用于在额外的窗口中生成“监控”输出。

  1. 不幸的是,它不会自动向下滚动到最近的一行。怎么了?
  2. 因为我对 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!")
4

2 回答 2

38

cls.text.see(Tkinter.END)在调用 insert 之后添加一条语句。

于 2009-05-01T14:50:48.380 回答
4

对于那些可能想尝试绑定的人:

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()
于 2013-07-19T14:53:24.870 回答