python 3.x - tkinter filedialog with statement -
i'd store file. reason, use filedialog 'with' statement. long chose file , save it, works fine. however, when filedialog canceled, following error:
file "...\src\test.py", line 7, in filedialog.asksaveasfile(mode='w') myfile: attributeerror: enter
is there work arround accomplish 'with' statement?
import tkinter tk tkinter import filedialog root = tk.tk() filedialog.asksaveasfile(mode='w') myfile: myfile.write('test') root.mainloop()
thanks clarification, zwer . think try...finally block convenient solution purpose. overcome problem follows:
import tkinter tk tkinter import filedialog def test(): root = tk.tk() myfile = filedialog.asksaveasfile(mode='w') if not myfile: return try: myfile.write('test') finally: myfile.close() root.mainloop() test()
Comments
Post a Comment