簡體   English   中英

Tkinter使用asksaveasfile保存文本文件

[英]Tkinter save a text file using asksaveasfile

我正在嘗試將一大堆變量保存到文本文檔中,但是當我使用asksaveasfile或asksaveasfilename時,單擊“保存”按鈕時出現錯誤。

代碼的相關部分(它選擇了大約一百個不同的變量,但是為了節省空間,我不會全部寫出它們)是:

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>

您沒有在調用asksaveasfilename() 您只是在引用它。 您需要添加括號:

text_file=open(asksaveasfilename())

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM