mbdrian
mbdrian

Reputation: 470

Insert string from file to TextArea Tkinter Python

def isiDataFile(self,namaFile):
    isiFile = open(namaFile)
    content = isiFile.read().lower()
    words = re.findall('\w+',content)

    print words

    self.textFile.delete('1.0',END)
    for i in words:
        self.textFile.insert('1.0',i+"\n")

    isiFile.close()

I wanna print some string from file .txt to textArea (Tkinter gui). Example, my string is

"ular melingkar, lalu terbang. harimau berjalan di atas air. eh, kenapa hujan atas? bukan sungai mendaki"

but, when i insert the string to textarea, the result :

>>> mendaki, sungai, bukan, atas, hujan, kenapa, eh, air, atas, di, berjalan, harimau, terbang, lalu, melingkar, ular.

it look, somehow, be reverse.

Upvotes: 0

Views: 2126

Answers (1)

dusty
dusty

Reputation: 885

The reason the order of the words is reversing is that you are inserting each word at the beginning:

    self.textFile.insert('1.0',i+"\n")

I would take words and build a single string, for example:

words = " ".join(words)

and then do the insert all at once instead of within a for-loop.

The other option would be to perform each insert within the for-loop at the end, rather than the beginning:

for word in words:
    self.textFile.insert(END, word)

Upvotes: 2

Related Questions