pythontkinterfiledialog

"File Name Not Valid" Python Tkinter


I'm making a simple text editor and whenever I try to save a file it always gives me this error.

enter image description here

I'm saving it as a normal text file and I cant find the reason why this is happening. I've tried saving it to a different location, made some debugging attempts. Nothing. Could someone help me out?

Code:

from tkinter import* 
from tkinter import filedialog 
from tkinter import font 

root = Tk() 
root.title("NewNotepad")
root.geometry("1200x660") 

def new_file(): 
    my_text.delete("1.0", END)
    root.title('New File')
    status_bar.config(text="New File") 

def open_file(): 
     my_text.delete("1.0", END)
     text_file = filedialog.askopenfilename(title="Open File", filetypes=(("Text Files", "*txt"), ("HTML Files", "*html")))  
     name = text_file 
     status_bar.config(text=name) 
     text_file = open(text_file, 'r')
     stuff = text_file.read() 
     my_text.insert(END, stuff) 
     text_file.close() 

def save_as_file(): 
    text_file = filedialog.asksaveasfilename(defaultextension=".*", title="Save File", initialdir="C:/gui/", filetypes=(("Text Files", "*txt"), ("HTML Files", "*html"))) 
    if text_file: 
        name = text_file 
        name = name.replace("C:/gui/", "") 
        root.title(f'{name} - NewNotepad') 
        text_file = open(text_file, 'w') 
        text_file.write(my_text.get(1.0, END))  
        text_file.close() 

my_frame = Frame(root)
my_frame.pack(pady=5)

text_scroll = Scrollbar(my_frame) 
text_scroll.pack(side=RIGHT, fill=Y) 

my_text = Text(my_frame, width=97, height=25, font=("Helvetica", 16), selectbackground="Light Blue", selectforeground="Black", undo=True, yscrollcommand=text_scroll.set) 
my_text.pack() 

text_scroll.config(command=my_text.yview)

my_menu =  Menu(root)
root.config(menu=my_menu)

file_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="File", menu=file_menu)
file_menu.add_command(label="New", command=new_file) 
file_menu.add_command(label="Open", command=open_file)
file_menu.add_command(label="Save")
file_menu.add_command(label="Save As", command=save_as_file) 
file_menu.add_separator() 
file_menu.add_command(label="Exit", command=root.quit) 

edit_menu = Menu(my_menu, tearoff=False)
my_menu.add_cascade(label="Edit", menu=edit_menu)
edit_menu.add_command(label="Cut") 
edit_menu.add_command(label="Copy")
edit_menu.add_separator()
edit_menu.add_command(label="Undo")
edit_menu.add_command(label="Redo")

status_bar = Label(text="Ready", anchor=E)
status_bar.pack(fill=X, side=BOTTOM, ipady=5) 

root.mainloop() 

Solution

  • It may be because you used defaultextension=".*" in asksaveasfilename(). When you enter a filename without extension, .* will be appended to the filename and so the filename is an invalid filename.

    Change defaultextension=".*" to defaultextension=".txt", for example, if you want to save the file with extension .txt if you do not enter file extension in the file dialog.