我们从Python开源项目中,提取了以下14个代码示例,用于说明如何使用tkinter.filedialog.asksaveasfile()。
def saveBox( self, title=None, fileName=None, dirName=None, fileExt=".txt", fileTypes=None, asFile=False): self.topLevel.update_idletasks() if fileTypes is None: fileTypes = [('all files', '.*'), ('text files', '.txt')] # define options for opening options = {} options['defaultextension'] = fileExt options['filetypes'] = fileTypes options['initialdir'] = dirName options['initialfile'] = fileName options['title'] = title if asFile: return filedialog.asksaveasfile(mode='w', **options) # will return "" if cancelled else: return filedialog.asksaveasfilename(**options)
def exportNetwork(self): export = {} export['apiConnectionData'] = self.apiConnection.data export['pool'] = [] for x in range(len(self.pool.pool)): export['pool'].append({'gene': self.pool.pool[x].gene, 'fitness': self.pool.pool[x].fitness}) export['currentGeneration'] = self.pool.currentGeneration export['averageFitness'] = self.pool.averageFitness export['bestDNAgene'] = self.pool.bestDNA.gene export['bestDNAfitness'] = self.pool.bestDNA.fitness try: filen = filedialog.asksaveasfile().name f = open(filen, 'w') f.write(json.dumps(export)) f.close() except: messagebox.showerror("Error", "Something went wrong")
def file_save(self): """Abre un diálogo para guardar el contenido que se encuentra en el editor, dentro de un archivo especificado por el usuario. En caso de que el archivo ya haya sido guardado anteriormente, esta función sobreescribirá el contenido del archivo. Función basada en código encontrado en stack overflow: http://stackoverflow.com/a/19476284 """ if self.filename == '': f = tk.filedialog.asksaveasfile(mode='w', defaultextension=".frida") else: f = open(self.filename, 'w') if f is None: # asksaveasfile regresa `None` si diálogo se cierra con "cancel". return text_save = str(self.text.get(1.0, tk.END)) f.write(text_save) f.close()
def getSaveFile(strdir='C:\\', strfile = 'file.txt', strtitle='Save file as ...'): """Pop up a file dialog window to select a file to save.""" options = {} options['initialdir'] = strdir options['title'] = strtitle #options['defaultextension'] = '.txt' #options['filetypes'] = [('all files', '.*'), ('text files', '.txt')] options['initialfile'] = strfile #options['parent'] = root root = tkinter.Tk() root.withdraw() file_path = filedialog.asksaveasfile(**options) #same as: file_path = tkinter.filedialog.asksaveasfile(**options) return file_path
def save_file(self, data, overwrite=False): if overwrite and self.controller.has_saved: f = open(self.controller.saved_path, 'w') else: f = tkfd.asksaveasfile(mode="w") if f is None: # If dialogue was closed by the 'cancel' return f.write(data) self.controller.has_saved = True self.controller.saved_path = f.name f.close()
def saveAsHK(self): f = filedialog.asksaveasfile(mode='w', defaultextension='.txt') t = text.get(0.0, END) try: f.write(t.rstrip()) except: showerror(title ="UPS", message="Could not save file")
def saveAs(): f = filedialog.asksaveasfile(mode='w', defaultextension='.txt') t = text.get(0.0, END) try: f.write(t.rstrip()) except: showerror(title ="UPS", message="Could not save file")
def save_as(results_display): file = filedialog.asksaveasfile( mode='w', defaultextension='.txt', filetypes=(('Tab delimited text file', '*.txt'), ('all files', '*.*'))) # Exit function if user presses cancel button. if file is None: return # Determine the order that the columns appear on the screen. order_map = [] # '#all' means that the columns are in their original order and all # are being shown. if '#all' in results_display['displaycolumns']: order_map = list(range(len(ColumnSelect.column_names))) else: for title in results_display['displaycolumns']: order_map.append(ColumnSelect.column_names.index(title)) for item in results_display.selection(): values = results_display.item(item=item, option='values') for i in range(len(order_map)): if order_map[i] < len(values): file.write(values[order_map[i]]) if i != len(order_map) - 1: file.write('\t') if item != results_display.selection()[-1]: file.write('\n') file.close()
def save_history(self): f = filedialog.asksaveasfile(mode='w', filetypes=[self.get_filetype()], defaultextension=self.get_filetype()[1]) if f is None: return for movie in self.historyList[self.searchMode]: f.write(movie+"\n") f.close()
def file_save_as(self): """Abre un diálogo para guardar el contenido que se encuentra en el editor, dentro de un archivo especificado por el usuario Función basada en código encontrado en stack overflow: http://stackoverflow.com/a/19476284 """ f = tk.filedialog.asksaveasfile(mode='w', defaultextension=".frida") if f is None: # asksaveasfile regresa `None` si el diálogo se cierra con "cancel". return text_save = str(self.text.get(1.0, tk.END)) self.filename = f.name # settea el nombre de archivo actual f.write(text_save) f.close()
def save_preset(self): file = asksaveasfile(filetypes=[("Synth presets", "*.ini")]) cf = ConfigParser(dict_type=collections.OrderedDict) # general settings cf.add_section("settings") cf["settings"]["samplerate"] = str(self.samplerate_choice.get()) cf["settings"]["rendering"] = self.rendering_choice.get() cf["settings"]["to_speaker"] = ",".join(str(v+1) for v in self.to_speaker_lb.curselection()) cf["settings"]["a4tuning"] = str(self.a4_choice.get()) # oscillators for num, osc in enumerate(self.oscillators, 1): section = "oscillator_"+str(num) cf.add_section(section) for name, var in vars(osc).items(): if name.startswith("input_"): cf[section][name] = str(var.get()) # adsr envelopes for num, filter in enumerate(self.envelope_filters, 1): section = "envelope_"+str(num) cf.add_section(section) for name, var in vars(filter).items(): if name.startswith("input_"): cf[section][name] = str(var.get()) # echo cf.add_section("echo") for name, var in vars(self.echo_filter).items(): if name.startswith("input_"): cf["echo"][name] = str(var.get()) # tremolo cf.add_section("tremolo") for name, var in vars(self.tremolo_filter).items(): if name.startswith("input_"): cf["tremolo"][name] = str(var.get()) # arpeggio cf.add_section("arpeggio") for name, var in vars(self.arp_filter).items(): if name.startswith("input_"): cf["arpeggio"][name] = str(var.get()) cf.write(file) file.close()
def file_save(event=None): # a+ --> add stuff to the end of the file you are writing to f = filedialog.asksaveasfile(mode='a+', defaultextension='.txt') if f is None: return # lesson_details_to_save = str('\n' + str((subject_entry.get(), hours_entry.get(), amount_entry.get()))) lessons_to_save = notes_text.get(0.0, tk.END) f.write(lessons_to_save) f.close()