i have entry widget user can type in file location, , underneath "save" button , "load" button. depending on button clicked, file specified in entry widget either opened writing, or reading.
this works fine , dandy.
now want add "browse" button, user can click open file dialog select file. when file selected, filename copied entry. there on, save , load buttons should work fine.
however, can't figure out how file dialog work both reading file , writing. can't use tkfiledialog.asksaveasfilename
because that's going complain user if file exists (which, if user intends "load", should) , tkfiledialog.askloadasfilename
function doesn't let user select file doesn't exist yet (which, if user intends "save", should fine well).
is possible create dialog displays neither of these functionalities?
is you're looking for:
from tkinter import * tkinter.filedialog import * root = tk() root.title("save , load") root.geometry("600x500-400+50") def importfiles(): try: filenames = askopenfilenames() global file 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 1 dialog returns filename can used both saving , loading."
can import file names using dialog window; remove selected file name list (additional function); open file selected; , finally, write , save them.
p.s.: there may bugs in code, think, algorithm question asks.
Comments
Post a Comment