tkinter text 获取text每一行值并存入列表中

首先import tkinter as tk

import tkinter as tk

然后创建窗口和text组件
root=tk.Tk()#窗口 root.title("获取内容") text=tk.Text(root,width=30,height=10)#创建text组件 text.pack()

再加上一个按钮来控制值的获取
button=tk.Button(root,text="确定",command=getData) button.pack()

编写按钮绑定的函数getData,获取text内容并将其按行存入列表
text_content = []#用来储存每一行内容的列表 def getData(): #获取text全部内容并去除内容中的空格,用split将内容以每一行末尾的\n分割成一个列表 text_content = (text.get("0.0","end").replace(" ","")).split("\n") text_content.pop()#列表最后一个元素是空删除它 print(text_content) root.mainloop()

【tkinter text 获取text每一行值并存入列表中】

    推荐阅读