0

I have an entry widget where the user can type in a file location, and underneath that a "save" button and a "load" button. Depending on which button is clicked, the file specified in the entry widget is either opened for writing, or for reading.

This all works fine and dandy.

Now I want to add a "browse" button, which the user can click to open a file dialog to select a file. When a file is selected, the filename is copied into the entry. From there on, the save and load buttons should work fine.

However, I can't figure out how to get the file dialog to work for both reading a file and writing. I can't use tkFileDialog.asksaveasfilename because that's going to complain to the user if a file already exists (which, if the user intends to "load", it should) and the tkFileDialog.askloadasfilename function doesn't let the user select a file which doesn't exist yet (which, if the user intends to "save", should be fine as well).

Is it possible to create a dialog which displays neither of these functionalities?

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
acdr
  • 4,538
  • 2
  • 19
  • 45
  • Ask directory not filename ! http://stackoverflow.com/questions/11295917/how-to-select-a-directory-and-store-the-location-using-tkinter-in-python – dsgdfg Jul 21 '16 at 14:45
  • Asking for a directory doesn't let you pick files at all. ("The folder C:\test.txt can't be used. Please choose another folder.") – acdr Jul 21 '16 at 14:48
  • Ask filename for read if you want save use ask directory. Create top level window with 2 button `SAVE`, `LOAD`. it is not simple : if file is locked(mean opening), if exists, if overwrite etc... – dsgdfg Jul 21 '16 at 14:50
  • The entire point is that I would like one dialog that can do both. If I wanted what you're suggesting, I'd just use `asksaveasfilename` and `askloadasfilename`. – acdr Jul 21 '16 at 14:52
  • Please add some code to your question. – Parviz Karimli Jul 21 '16 at 22:44
  • @acdr "The entire point is that I would like one dialog that can do both." -Do you want one file dialog to do both saving and loading? Or do you want a program that you can write & save, and then you can open & load it, and write & save it again? – Parviz Karimli Jul 21 '16 at 22:47
  • @Parviz: I don't want one dialog to do both saving and loading. I want one dialog which returns a filename that can be used for both saving and loading. Also, I don't think adding any code to my question would elucidate anything in this case. – acdr Jul 22 '16 at 07:20

1 Answers1

0

Is this what you're looking for:

from tkinter import *
from tkinter.filedialog import *
root = Tk()
root.title("Save and Load")
root.geometry("600x500-400+50")

def importFiles():
    try:
        filenames = askopenfilenames()
        global file
        for file in filenames:
            fileList.insert(END, file)
    except:
        pass

def removeFiles():
    try:
        fileList.delete(fileList.curselection())
    except:
        pass

def openFile():
    try:
        text.delete(END)
        fob = open(file, 'r')
        text.insert(0.0, fob.read())
    except:
        pass

def saveFile():
    try:
        fob = open(file, 'w')
        fob.write(text.get(0.0, 'end-1c'))
        fob.close()
    except:
        pass

listFrame = Frame(root)
listFrame.pack()

sby = Scrollbar(listFrame, orient='vertical')
sby.pack(side=RIGHT, fill=Y)

fileList = Listbox(listFrame, width=100, height=5, yscrollcommand=sby.set)
fileList.pack()

sby.config(command=fileList.yview)

buttonFrame = Frame(root)
buttonFrame.pack()

importButton = Button(buttonFrame, text="Import", command=importFiles)
importButton.pack(side=LEFT)

removeButton = Button(buttonFrame, text="Remove", command=removeFiles)
removeButton.pack(side=LEFT)

openButton = Button(buttonFrame, text="Open", command=openFile)
openButton.pack(side=LEFT)

saveButton = Button(buttonFrame, text="Save", command=saveFile)
saveButton.pack(side=LEFT)

text = Text(root)
text.pack()

root.mainloop()

"I want one dialog which returns a filename that can be used for both saving and loading."
You can import file names using a dialog window; remove the selected file name from the list (additional function); open the file you selected; and finally, write and save them.
P.S.: There may be some bugs in my code, but I think, the algorithm does what the question asks.

Parviz Karimli
  • 1,247
  • 1
  • 14
  • 26