2016-07-15 29 views
1

我有一个程序,它将输入到文本列表和索引列表中的句子压缩;我也有一个程序,在使用Tkinter浏览你的库并保存时创建一个文本。当与其他代码一起使用时,Tkinter保存到文件不起作用

这两段代码都单独工作,但是当我一起使用它们以尝试使用tkniter将单词列表保存到文本文件时,该代码只是无休止地运行,而不会引起tkinter,它声称是“调试“。请帮忙,因为我看不出这个代码有什么问题。谢谢。

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) 

回答

1

您的代码在我的Ubuntu 14.04上运行得非常好。但最后一行是错误的。

wordFile.write(file_text) 

write需要一个string,但你给人一种list它。

要么使用

wordFile.write(str(file_text)) 

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