首页 > 解决方案 > 如何在 tkinter 的文本小部件中获取一行字符?

问题描述

我的 Text 小部件包含:

This is the first line
This is the Second line
This is the Third line

以及如何检索由每一行分隔的整个字符?,请帮助我

标签: python-3.xtkinter

解决方案


from tkinter import Tk, Text, END

root = Tk()
text = Text(root)
text.insert(0.0,
"""This is the first line
This is the Second line
This is the Third line""")
text.pack()
lines = text.get(0.0, END).split("\n") #That's the line you need
print(lines)

root.mainloop()

命令行输出:['This is the first line', 'This is the Second line', 'This is the Third line', '']

它在最后留下一个空白项目,但如果您使用以下内容,您可以将其删除:

lines = text.get(0.0, END).split("\n")[:-1]

这将输出:['This is the first line', 'This is the Second line', 'This is the Third line']


推荐阅读