Python 如何在Tkinter消息窗口中自动滚动
我编写了下面的类,用于在额外的窗口中生成“监视”输出Python 如何在Tkinter消息窗口中自动滚动,python,qt,qt4,tkinter,ipython,Python,Qt,Qt4,Tkinter,Ipython,我编写了下面的类,用于在额外的窗口中生成“监视”输出 不幸的是,它不会自动向下滚动到最近的一行。怎么了 我在Tkinter和ipython方面也有问题:qt4的等效实现会是什么样子 代码如下: import Tkinter class Monitor(object): @classmethod def write(cls, s): try: cls.text.insert(Tkinter.END, str(s) + "\n") cls.text.updat
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!")
在调用insert的语句后面添加一条语句
cls.text。请参阅(Tkinter.END)
。对于那些可能想尝试绑定的人:
def callback():
text.see(END)
text.edit_modified(0)
text.bind('<<Modified>>', callback)
def callback():
正文.见(完)
text.edit\u modified(0)
text.bind(“”,回调)
小心点。正如@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()
将Tkinter作为tk导入
def showEnd(事件):
文本。请参见(tk.END)
text.edit_modified(0)#重要-或稍后不会调用。
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
root=tk.tk()
text=tk.text(根,换行=tk.WORD,高度=5)
text.insert(tk.END,“可以\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n显示\n \n显示\n \n丢失\n
text.edit_modified(0)#重要-或稍后不会调用。
text.pack()
text.bind(“”,showEnd)
button=tk.button(text='Show End',command=lambda:text.see(tk.End))
button.pack()
root.mainloop()
执行此操作时,请考虑可用性。例如,如果用户从底部向上滚动查看您不想自动滚动的内容。“Python:如果用户不手动滚动,则自动滚动滚动文本到末尾”为什么说它已损坏?这是一个记录在案的错误吗?您是否知道,
仅在窗口第一次从未修改转换为已修改时触发一次?除非用清除该标志,否则将无法再次获取该事件。edit\u modified(True)
。图:不解释这一点,也不解释,但如果仔细阅读会解释。调整以上答案,谢谢!顺便说一句,它是。编辑\u修改(False)