Cannot seem to figure out the below Tkinter message. Usually doing a quick search will provide answers but this time I seem to miffed the search engines as to what might be causing the below error. Curious to know if I am missing a Python package or line 25 below is used in an older version of Python and it has been updated to a newer command.
I am importing the following packages into the script:
from tkinter import *
from tkinter import filedialog
The function is suppose to save any typed text put into a text area. It does save the file but the file is empty.
Thanks,
Kurt
C:Userskurt>python –version Python 3.10.4
def saveFiles(): filename = filedialog.asksaveasfile( mode='w', title="Save a File", defaultextension=".txt" ) filename.config(mode='w') ------------> **This is line 25** pathh.insert(END, filename) data = str(txtarea.get(1.0, END)) filename.write(data) filename.close() Exception in Tkinter callback Traceback (most recent call last): File "C:Program FilesWindowsAppsPythonSoftwareFoundation.Python.3.10_3.10.1264.0_x64__qbz5n2kfra8p0libtkinter__init__.py", line 1921, in __call__ return self.func(*args) File "c:UserskurtDocumentsScriptsTKinterfileExplorerReadFile.py", line 25, in saveFiles filename.config(mode='w') AttributeError: '_io.TextIOWrapper' object has no attribute 'config'
Advertisement
Answer
The error is saying that the object returned by the asksaveasfile
method doesn’t have a config
method.
tkinter.filedialog.asksaveasfile(mode='w', **options)
ΒΆ Create a SaveAs dialog and return a file object opened in write-only mode.
When you call the asksaveasfile
method, it automatically returns a file object in write mode already so there is no need for any further configuration to write to the file. If you were to omit the line throwing the error, your code should work the way you intended.