1
如果我有充滿Tkinter的文本插件的以下內容:Tkinter的文本組件,遍歷行
/path/to/file/1.txt
/path/to/file/2.txt
/path/to/file/3.txt
有遍歷所有線路(例如直接的方式,打開一個文件,執行行動和寫作)?
如果我有充滿Tkinter的文本插件的以下內容:Tkinter的文本組件,遍歷行
/path/to/file/1.txt
/path/to/file/2.txt
/path/to/file/3.txt
有遍歷所有線路(例如直接的方式,打開一個文件,執行行動和寫作)?
text_widget.get('1.0', 'end-1c')
以字符串形式返回整個文本內容。使用str.splitlines()
分割它。
from tkinter import *
def iterate_lines():
for line in t.get('1.0', 'end-1c').splitlines():
# Iterate lines
if line:
print('path: {}'.format(line))
root = Tk()
t = Text(root)
t.insert(END, '/path/to/file/1.txt\n/path/to/file/2.txt\n/path/to/file3.txt\n')
t.pack()
Button(root, text='iterate', command=iterate_lines).pack()
root.mainloop()