Adam Thomasson
Adam Thomasson

Reputation: 15

Tkinter saving to file not working when used with other code

I have a program which compresses a sentence that you input into a list of words and an index list; I also have a program which creates a text while using Tkinter to browse your library and save it.

Both of these pieces of code work individually, however when i use them together in an attempt to save the list of words to a text file using tkniter the code just runs endlessly without bringing up tkinter and it claims to be "debugging".Please help as i cannot see anything wrong with this code. Thanks.

text=input("Type a sentence you would like to compress.").lower()
first_list=text.split()

second_list=list(set(first_list))

third_list=[]
for x in range(len(first_list)):
    for y in range(len(second_list)):
        if first_list[x]==second_list[y]:
            third_list.append(y)

simple_sentence=second_list
index_list=third_list

file_text=simple_sentence

import tkinter as tk
from tkinter import filedialog

root=tk.Tk()
root.withdraw()
file_path=filedialog.asksaveasfilename()

with open(file_path+".txt", "a") as wordFile:
    wordFile.write(file_text)

Upvotes: 1

Views: 47

Answers (2)

user9748614
user9748614

Reputation: 1

>>> tk.rspl()

That should be the last line

Upvotes: 0

Gábor Erdős
Gábor Erdős

Reputation: 3689

Your code runs perfectly fine on my Ubuntu 14.04. The very last line however is wrong.

wordFile.write(file_text)

write expects a string, but you are giving a list to it.

Either use

wordFile.write(str(file_text))

or

wordFile.write(" ".join(file_text))

Upvotes: 1

Related Questions