简体   繁体   English

如何在tkinter文本小部件中获取最后一行的内容(Python 3)

[英]How to get the contents of last line in tkinter text widget (Python 3)

I am working on a virtual console, which would use the systems builtin commands and then do the action and display output results on next line in console. 我正在使用虚拟控制台,它将使用系统内置命令,然后执行操作并在控制台的下一行显示输出结果。 This is all working, but how do I get the contents of the last line, and only the last line in the tkinter text widget? 一切正常,但是如何获取tkinter文本小部件中的最后一行的内容以及最后一行的内容? Thanks in advance. 提前致谢。 I am working in python 3. I have treed using text.get(text.linestart, text.lineend) To no avail. 我正在python 3中工作。我已经使用text.get(text.linestart, text.lineend)进行了text.get(text.linestart, text.lineend)但无济于事。 Have these been deprecated? 这些已经过时了吗? It spits out an error saying that AttributeError: 'Text' object has no attribute 'linestart' 它吐出一个错误,指出AttributeError: 'Text' object has no attribute 'linestart'

You can apply modifiers to the text widget indicies, such as linestart and lineend as well as adding and subtracting characters. 您可以将修饰符应用于文本小部件索引,例如linestartlineend以及添加和减少字符。 The index after the last character is "end" . 最后一个字符之后的索引是"end"

Putting that all together, you can get the start of the last line with "end-1c linestart" . 将所有内容放在一起,可以使用"end-1c linestart"获得最后一行的开始。

Test widget has a see(index) method. 测试窗口小部件具有see(index)方法。 text.see(END) will scroll the text to the last line. text.see(END)会将文本滚动到最后一行。

Why not just retrieve the all the content from the textwidget using 为什么不使用以下命令从textwidget中检索所有内容?

some_text = text.get()

Then do some simple python text manipulation using 然后使用进行一些简单的python文本操作

lines = some_text.split("\n")
last_line = lines[-1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM