Python Tkinter - 如何在文本框的开头插入文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18346206/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python Tkinter - How to insert text at the beginning of the text box?
提问by user2662241
I am using Python 2.7.5 and Tkinter. I am writing status messages into a text widget. To insert I am using:
我正在使用 Python 2.7.5 和 Tkinter。我正在将状态消息写入文本小部件。要插入我正在使用:
text_widget.insert(INSERT, 'my status here.\n')
This works fine. However, each status is added after the previous status. How do I insert new lines at the top of the Text
widget so that the most recent status is at the top, while keeping the previous status messages below?
这工作正常。但是,每个状态都添加在前一个状态之后。如何在Text
小部件的顶部插入新行,以便最近的状态位于顶部,同时保留之前的状态消息在下方?
Thanks.
谢谢。
回答by Brionius
text_widget.insert('1.0', 'my status here.\n')
In short, the text widget can be indexed using a string notation of the form "lineNum.columnNum"
, as well as using the special indices like Tkinter.INSERT
.
简而言之,可以使用表单的字符串表示法"lineNum.columnNum"
以及使用特殊索引(如Tkinter.INSERT
.
I would recommend reading effbot's documentation for the Tkinter text widget, or New Mexico Tech's Tkinter Handbook, which I've found has fewer examples and explanations, but is more complete as an API reference.
我建议阅读effbot 的 Tkinter 文本小部件文档,或新墨西哥理工学院的 Tkinter 手册,我发现它的示例和解释较少,但作为 API 参考更完整。