簡體   English   中英

Tkinter文本小部件,迭代線

[英]Tkinter Text Widget, iterating over lines

如果我有一個填充了以下內容的tkinter Text小部件:

/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()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM