对话框tk.asksaveasfile:TypeError:预期的str,字节或os.PathLike对象,而不是_io.TextIOWrapper

时间:2018-12-25 14:14:40

标签: python tkinter save-as

我正在使用Python3和tkinter技术编写代码。我试图将粘贴的文本保存在驱动器中文件的GUI的左框架(textArea1)中,但是收到TypeError消息。粘贴的文本不会保存在文件中。所需文件模式='w',类型(* .txt),编码='utf-8'。有人可以帮忙吗?我是编码新手。 我正在尝试构建基于文本的语言学习工具。用户在左侧框架上粘贴复制的文本或加载此类文本以供研究。单击按钮,右框架列出了用户必须使用词典查找的未知单词。底部的第三个框架将测试用户对创建的词汇表的了解。

我尝试了多种方法来解决 saveas 问题,但失败了。我需要用户选择将粘贴的文件保存在驱动器中的位置和名称。将文件另存为txt很重要,如果文本包含特殊字符,则encoding = utf-8。

from tkinter import filedialog, re
from tkinter import *
from tkinter.scrolledtext import *
import os

input_Value = ''

# FUNCTIONS
def doNothing():
    pass

def open_File():
    filename = filedialog.askopenfilename(initialdir='/', title='Select File ',filetypes=(("Text file", "*.txt"), ("All files", "*.*")))
    file = open(filename, encoding='UTF-8')
    contents = file.readlines()
    contents = ''.join(contents) # this is a string
    textArea1.insert(END, contents)

    text = re.findall(r'\w+', contents)
    with open('known_words.txt', encoding='UTF-8') as file:
        content=file.read()
        mySet=set(text)
        finList=list(mySet)
        print(finList)
        for word in finList:
            if word in contents:
                textArea2.insert(END, word+' = '+'\n')
        file.close()

def retrieveInput():
    global input_Value
    input_Value = textArea1.get('1.0', 'end-1c')
    print(input_Value)

def save_File_As():
    filename = filedialog.asksaveasfile(initialdir = "/", filetypes = (("all files","*.*"),("jpeg files","*.jpg")))

##    if file != None:
##        file.write(input_Value)
##        file.close()

    file=open(filename, mode='w', encoding="UTF-8")
    file.write(input_Value)
    file.close()

##    with open("deutsch2.txt", 'w', encoding="UTF-8") as txt_file:
##        print(filename)
##        txt_file.write(input_Value)

# Root for main window

0 个答案:

没有答案