在GUI输入字段中读取文本文件

时间:2013-11-28 20:20:07

标签: python file text tkinter

我想知道一种阅读文本文件并将其内容添加到文本框中的方法。我在tkinter中这样做,所以我需要在Python中获取文本,并使用tkinter将其放入文本框中,这将是很棒的。提前谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用withopen打开文件,然后Text.insert将其内容放入文本框中。

以下是基本演示:

from Tkinter import Text, Tk
r = Tk()
t = Text()
t.grid()
with open("/path/to/file") as myfile:
    t.insert("1.0", myfile.read())
r.mainloop()
相关问题