Search code examples
pythonuser-interfacetkintertkinter-entrysavefiledialog

Is there a way to populate an entry box with the last saved file using python tkinter?


I am a beginner to python and unable to figure out how to show the last saved file in the GUI using python tkinter.

Implementing the ability for a user to save their custom input into a csv file. I would like to after they save their file, to have the last saved file path populate in the entry box on the GUI.

Any ideas on how I can approach populating that entry box with the last saved file?


Solution

  • Here's a basic example app that will call asksaveasfilename when you click a button, and update the text in an Entry widget if a filename is provided

    import tkinter as tk
    from tkinter import ttk
    from tkinter.filedialog import asksaveasfilename
    
    
    def save_file():
        if filename := asksaveasfilename():
            entry_var.set(filename)  # update the entry text
    
    
    root = tk.Tk()
    entry_var = tk.StringVar()  # updates the entry text automatically when modified
    entry = ttk.Entry(root, textvariable=entry_var)
    entry.pack()
    btn = ttk.Button(root, command=save_file, text="Save")
    btn.pack()
    
    
    if __name__ == '__main__':
        root.mainloop()  # run the app