I'm trying to run sample tkinter
a code but showing an error
this is the code that i tried,
def save_as():
global fo
fo = asksaveasfile(initialfile = 'Untitled.txt', defaultextension=".txt",filetypes=[("All Files","*.*"),("Text Documents","*.txt"),("Word files",".docx")])
print(os.path.basename(fo).split('/')[-1])
def sample():
btn = Button(root, text='click', command=save_as)
btn.pack()
root = tk.Tk()
sample()
root.mainloop()
But i geting an error like this
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\tkinter\__init__.py", line 1921, in __call__
return self.func(*args)
File "c:\Users\DELL\Desktop\Ismail\test.py", line 9, in save_as
print(os.path.basename(fo).split('/')[-1])
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ntpath.py", line 242, in basename
return split(p)[1]
File "C:\Program Files\WindowsApps\PythonSoftwareFoundation.Python.3.10_3.10.3056.0_x64__qbz5n2kfra8p0\lib\ntpath.py", line 211, in split
p = os.fspath(p)
TypeError: expected str, bytes or os.PathLike object, not TextIOWrapper
can anyone help
Note that fo
is an open file object, so you cannot use it in os.path.basename()
.
Use asksaveasfilename()
instead of asksaveasfile()
.