如何在 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!")
I wrote the following class for producing "monitoring" output within an extra window.
- Unfortunately it doesn't scroll automatically down to the most recent line. What is wrong?
- As I also have problems with Tkinter and ipython: how would an equivalent implementation with qt4 look like?
Here is the 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()
Usage:
Monitor.write("Hello World!")
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在调用 insert 的后面添加一条语句
cls.text.see(Tkinter.END)
。Add a statement
cls.text.see(Tkinter.END)
right after the one calling insert.对于那些可能想尝试绑定的人:
请小心。 正如@BryanOakley 指出的,Modified 虚拟事件仅被调用一次,直到被重置。 考虑如下:
To those who might want to try binding:
Just be careful. As @BryanOakley pointed out, the Modified virtual event is only called once until it is reset. Consider below: