-1
我试图将大量变量保存到文本文档中,但当我单击“保存”按钮时出现错误,当我使用asksaveasfile或asksaveasfilename时。Tkinter使用asksaveasfile保存文本文件
代码的相关部分(它都会约一百名不同的变量,但为了节省空间,我不会写所有的人)是:
from tkinter import *
from tkinter.filedialog import *
def save_doc():
text_file=open(asksaveasfile, mode ='w')
text_file.write(nombrePatrocinado+"\n")
text_file.write(apellidoPaternoPatrocinado+"\n")
text_file.close()
saveDocBTN=Button(text="Save", command=save_doc)
saveDocBTN.grid(row=0,column=6)
当我使用它,它说
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/home/juanelo/Downloads/launcher.py", line 662, in save_doc
text_file=open(asksaveasfile, mode ='w')
TypeError: invalid file: <function asksaveasfile at 0xb5e973d4>
另外一个我想是非常相同:
from tkinter import *
from tkinter.filedialog import *
def save_doc():
text_file=open(asksaveasfilename)
text_file.write(nombrePatrocinado+"\n")
text_file.write(apellidoPaternoPatrocinado+"\n")
text_file.close()
saveDocBTN=Button(text="Save", command=save_doc)
saveDocBTN.grid(row=0,column=6)
当我尝试,我得到
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python3.4/tkinter/__init__.py", line 1536, in __call__
return self.func(*args)
File "/home/juanelo/Downloads/launcher.py", line 662, in save_doc
text_file=open(asksaveasfilename)
TypeError: invalid file: <function asksaveasfilename at 0xb5df82b4>
我这样做,它打开的对话框中,但它显示了一个错误,当我尝试创建一个新的文件保存: FileNotFoundError:[错误2]没有这样的文件或目录:'/home/juanelo/Downloads/ddd.txt' –
你需要告诉python创建它,如果它不存在:'text_file = open(asksaveasfilename(),'w')' – zondo