1

I just started programming with Python and I'm trying to create a GUI using tkinter where it would ask the user to select a zip file and file destination to send it to after extracting. What I have noticed is that when a user re-enters a destination, it would still store the previous file directory as well. How do I prevent this?

import tkinter as tk
from tkinter import filedialog

# Setting the window size
screenHeight = 450
screenWidth = 350

root = tk.Tk()


# get the location of the zip file
def input_dir():
    input_filedir = filedialog.askopenfilename(initialdir='/', title='Select File',
                                                filetypes=(("zip", "*.zip"), ("all files", "*.*")))
    my_label.pack()
    return input_filedir


# get location of destination for file 
def output_dir():
    output_filename = filedialog.askdirectory()


# Setting the canvas size to insert our frames
canvas = tk.Canvas(root, height=screenHeight, width=screenWidth)
canvas.pack()

# Setting the frame size to insert all our widgets
frame = tk.Frame(root, bg='#002060')
frame.place(relwidth=1, relheight=1)

# button to get user to chose file directory
openFile = tk.Button(frame, text='Choose the file path you want to extract', command=input_dir)
openFile.pack(side='top')

# button to get destination path 
saveFile = tk.Button(frame, text="Chose the location to save the file", command=output_dir)
saveFile.pack(side='bottom')

extractButton = tk.Button(frame, text="Extract Now")


root.mainloop()

I have tried adding this line of code in the def input_dir function but it changed the positioning of the buttons. I'm still working on the code for extracting the zip.

for widget in frame.winfor_children():
    if isinstance(widget, tk.Label):
        widget.destroy()

Dave White
  • 31
  • 3

1 Answers1

1

The files/directories the user clicks are not saved really. Your variables input_filedir and output_filename are garbage collected when their respective functions are completed. If you are talking about how the dialog boxes go back to the most recent places opened in a dialog, that is not really something you can mess with too much. The easy answer is you can add the keyword 'initialdir' when you make the dialog to just pick where it goes like this:

output_filename = filedialog.askdirectory(initialdir='C:/This/sort/of/thing/')

The long answer is that filedialog.askdirectory actually creates an instance of filedialog.Directory and in that class, it saves that information for later in a method called _fixresult (but that method also does other things that are important.) You could overwrite this by doing something like this:

class MyDirectory(filedialog.Directory):
    def _fixresult(self, widget, result):
        """
        this is just a copy of filedialog.Directory._fixresult without
        the part that saves the directory for next time
        """
        if result:
            # convert Tcl path objects to strings
            try:
                result = result.string
            except AttributeError:
                # it already is a string
                pass
        self.directory = result # compatibility
        return result

def askdirectory (**options):
    "Ask for a directory, and return the file name"
    return MyDirectory(**options).show()

and then using your own askdirectory function instead of filedialog.askdirectory, but that is really convoluted so I recommend using initialdir instead if you can.

P.S. When a button is clicked, it calls the function you set after "command=" when you made the button; it seems you got that. But these functions are 'void', in that their return is just ignored. That is to say, the "return input_filedir" line does nothing.

Joel Toutloff
  • 464
  • 3
  • 4