If I have a tkinter Text widget filled with the following:
/path/to/file/1.txt
/path/to/file/2.txt
/path/to/file/3.txt
Is there a direct way to iterate over all lines (for instance, to open a file, perform an action, and write)?
text_widget.get('1.0', 'end-1c')
returns whole text content as string. Split it using 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()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With