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

Popular posts from this blog

networking - Vagrant-provisioned VirtualBox VM is not reachable from Ubuntu host -

c# - ASP.NET Core - There is already an object named 'AspNetRoles' in the database -

ruby on rails - ArgumentError: Missing host to link to! Please provide the :host parameter, set default_url_options[:host], or set :only_path to true -