首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Tkinter消息窗口中自动滚动

如何在Tkinter消息窗口中自动滚动
EN

Stack Overflow用户
提问于 2009-05-01 14:16:00
回答 2查看 21.4K关注 0票数 15

我编写了下面的类,用于在额外的窗口内生成“监视”输出。

不幸的是,

  1. 并没有自动滚动到最近的一行。怎么了?
  2. ,因为我也有Tkinter和ipython的问题: qt4的等效实现会是什么样子?

以下是代码:

代码语言:javascript
复制
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()

用法:

代码语言:javascript
复制
Monitor.write("Hello World!")
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2009-05-01 14:50:48

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

票数 40
EN

Stack Overflow用户

发布于 2013-07-19 14:53:24

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

代码语言:javascript
复制
def callback():
    text.see(END)
    text.edit_modified(0)
text.bind('<<Modified>>', callback)

小心点。正如@BryanOakley所指出的,修改后的虚拟事件只被调用一次,直到重置为止。考虑如下:

代码语言:javascript
复制
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()
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/811532

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档