与其他代码一起使用时,Tkinter保存到文件不起作用

时间:2016-07-15 14:14:19

标签: python tkinter

我有一个程序压缩你输入到单词列表和索引列表中的句子;我还有一个程序,可以在使用Tkinter浏览库并保存时创建文本。

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

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)

2 个答案:

答案 0 :(得分:1)

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

wordFile.write(file_text)

write需要string,但您要给list

使用

wordFile.write(str(file_text))

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

答案 1 :(得分:0)

>>> tk.rspl()

这应该是最后一行