我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用gtk.FileFilter()。
def on_open_clicked(widget, window, box): dialog=gtk.FileChooserDialog(title="?? Flash ??", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) swf_filter=gtk.FileFilter() swf_filter.set_name("Flash ??(*.swf)") swf_filter.add_pattern("*.[Ss][Ww][Ff]") dialog.add_filter(swf_filter) response = dialog.run() if response == gtk.RESPONSE_OK: filename = dialog.get_filename() dialog.destroy() window.remove(box) play_args = pre_play(filename) display_html(window, play_args) dialog.destroy()
def importFile(self, *args): fn = gtk.FileChooserDialog(title="Import File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) _filter = gtk.FileFilter() _filter.set_name("C++ Files") _filter.add_pattern("*.cpp") _filter.add_pattern("*.h") fn.add_filter(_filter) _filter = gtk.FileFilter() _filter.set_name("All Files") _filter.add_pattern("*") fn.add_filter(_filter) fn.show() resp = fn.run() if resp == gtk.RESPONSE_OK: text = open(fn.get_filename()).read() self.addNotebookPage(os.path.basename(fn.get_filename()), text) fn.destroy()
def on_open(self, action): chooser = gtk.FileChooserDialog(title="Open dot File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) chooser.set_default_response(gtk.RESPONSE_OK) chooser.set_current_folder(self.last_open_dir) filter = gtk.FileFilter() filter.set_name("Graphviz dot files") filter.add_pattern("*.dot") chooser.add_filter(filter) filter = gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") chooser.add_filter(filter) if chooser.run() == gtk.RESPONSE_OK: filename = chooser.get_filename() self.last_open_dir = chooser.get_current_folder() chooser.destroy() self.open_file(filename) else: chooser.destroy()
def keep_list_dialog(self, w): chooser = gtk.FileChooserDialog('Open..', None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) chooser.set_select_multiple(True) chooser.set_default_response(gtk.RESPONSE_OK) chooser.set_size_request(640,480) filter = gtk.FileFilter() filter.set_name("All Files") filter.add_pattern("*") chooser.add_filter(filter) response = chooser.run() if response == gtk.RESPONSE_OK: self.keep_list = chooser.get_filenames() chooser.destroy() if self.keep_list is not None: Thread(target=self._announce_keep_list).start()
def action_save_ngc(self, *arg) : filechooserdialog = gtk.FileChooserDialog(_("Save as ngc..."), None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) try : filt = gtk.FileFilter() filt.set_name("NGC") filt.add_mime_type("text/ngc") filt.add_pattern("*.ngc") filechooserdialog.add_filter(filt) filechooserdialog.set_current_folder(NGC_DIR) filechooserdialog.set_keep_above(True) filechooserdialog.set_transient_for(self.get_toplevel()) if filechooserdialog.run() == gtk.RESPONSE_OK: gcode = self.to_gcode() filename = filechooserdialog.get_filename() if filename[-4] != ".ngc" not in filename : filename += ".ngc" with open(filename, "wb") as f: f.write(self.to_gcode()) f.close() finally : filechooserdialog.destroy()
def action_loadCfg(self, *arg) : filechooserdialog = gtk.FileChooserDialog(_("Open a cfg file"), None, \ gtk.FILE_CHOOSER_ACTION_OPEN, \ (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) try: filt = gtk.FileFilter() filt.set_name(_("Config files")) filt.add_mime_type("text/xml") filt.add_pattern("*.cfg") filechooserdialog.add_filter(filt) filechooserdialog.set_current_folder(os.path.join(NCAM_DIR, CUSTOM_DIR)) filechooserdialog.set_keep_above(True) filechooserdialog.set_transient_for(self.get_toplevel()) if filechooserdialog.run() == gtk.RESPONSE_OK: self.add_feature(None, filechooserdialog.get_filename()) finally : filechooserdialog.destroy()
def on_open(self, action): chooser = gtk.FileChooserDialog(title="Open dot File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) chooser.set_default_response(gtk.RESPONSE_OK) filter = gtk.FileFilter() filter.set_name("Graphviz dot files") filter.add_pattern("*.dot") chooser.add_filter(filter) filter = gtk.FileFilter() filter.set_name("All files") filter.add_pattern("*") chooser.add_filter(filter) if chooser.run() == gtk.RESPONSE_OK: filename = chooser.get_filename() chooser.destroy() self.open_file(filename) else: chooser.destroy()
def _get_export_file_name(self): sel = gtk.FileChooserDialog("Save...", self.canvas.get_toplevel(), gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) sel.set_default_response(gtk.RESPONSE_OK) sel.set_local_only(True) sel.set_do_overwrite_confirmation(True) sel.set_current_name("Unnamed.pdf") filter = gtk.FileFilter() filter.set_name("Embedded PostScript") filter.add_mime_type("image/x-eps") sel.add_filter(filter) filter = gtk.FileFilter() filter.set_name("Portable Document Graphics") filter.add_mime_type("application/pdf") sel.add_filter(filter) filter = gtk.FileFilter() filter.set_name("Scalable Vector Graphics") filter.add_mime_type("image/svg+xml") sel.add_filter(filter) resp = sel.run() if resp != gtk.RESPONSE_OK: sel.destroy() return None file_name = sel.get_filename() sel.destroy() return file_name
def openFile(self, *args): if (not self.getModified()) or self.ask("There are unsaved files.\nContinue?"): fn = gtk.FileChooserDialog(title="Save File", action=gtk.FILE_CHOOSER_ACTION_OPEN, buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_OPEN,gtk.RESPONSE_OK)) _filter = gtk.FileFilter() _filter.set_name("Micro:Pi Files") _filter.add_pattern("*.%s" % SETTINGS['fileExtention']) fn.add_filter(_filter) _filter = gtk.FileFilter() _filter.set_name("All Files") _filter.add_pattern("*") fn.add_filter(_filter) fn.show() resp = fn.run() if resp == gtk.RESPONSE_OK: try: #text = open(fn.get_filename()).read() #try: #d = text.replace("\n", "") #d = base64.b64decode(d) #data = pickle.loads(d) #except: #data = pickle.loads(text) data = self.loadFilesFromFile(fn.get_filename()) mw = MainWin(data) yes = True mw.saveLocation = fn.get_filename() mw.setSaved() OPENWINDOWS.append(mw) except Exception as e: yes = False fn.destroy() if resp == gtk.RESPONSE_OK and not yes: self.message("File is not a Micro:Pi File")
def saveAs(self, *args): self.saveInternalOpenFiles() fn = gtk.FileChooserDialog(title="Save File As", action=gtk.FILE_CHOOSER_ACTION_SAVE, buttons=(gtk.STOCK_CANCEL,gtk.RESPONSE_CANCEL,gtk.STOCK_SAVE,gtk.RESPONSE_OK)) _filter = gtk.FileFilter() _filter.set_name("Micro:Pi Files") _filter.add_pattern("*.%s" % SETTINGS['fileExtention']) fn.add_filter(_filter) _filter = gtk.FileFilter() _filter.set_name("All Files") _filter.add_pattern("*") fn.add_filter(_filter) fn.show() resp = fn.run() #files = [] #for f in self.notebook: #fin = self.notebook.get_tab_label(f).get_children()[0].get_label() #tb = f.get_child().get_buffer() #txt = tb.get_text(*tb.get_bounds()) #files.append([fin, txt]) #data = base64.b64encode(pickle.dumps(files)) #data = "".join(data[i:i+64]+"\n" for i in xrange(0, len(data), 64)) if resp == gtk.RESPONSE_OK: fp = fn.get_filename() if fp[-(len(SETTINGS["fileExtention"])+1):] != "." + SETTINGS["fileExtention"]: fp += "." + SETTINGS["fileExtention"] #open(fp, 'w').write(data) self.saveLocation = fp self.save() self.setSaved() fn.destroy()
def openFile(self, widget=None, default=False): """Reads from a config file. If default=True, open the tint2 default config.""" self.new() if not default: chooser = gtk.FileChooserDialog("Open Config File", self, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) chooser.set_default_response(gtk.RESPONSE_OK) if self.curDir != None: chooser.set_current_folder(self.curDir) chooserFilter = gtk.FileFilter() chooserFilter.set_name("All files") chooserFilter.add_pattern("*") chooser.add_filter(chooserFilter) chooser.show() response = chooser.run() if response == gtk.RESPONSE_OK: self.filename = chooser.get_filename() self.curDir = os.path.dirname(self.filename) else: chooser.destroy() return chooser.destroy() else: self.filename = os.path.expandvars("$HOME/.config/tint2/tint2rc") self.curDir = os.path.expandvars("$HOME/.config/tint2") self.readTint2Config() self.generateConfig() self.updateStatusBar()
def saveAs(self, widget=None, event=None): """Prompts the user to select a file and then saves the generated config file.""" self.generateConfig() chooser = gtk.FileChooserDialog("Save Config File As...", self, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) chooser.set_default_response(gtk.RESPONSE_OK) if self.curDir != None: chooser.set_current_folder(self.curDir) chooserFilter = gtk.FileFilter() chooserFilter.set_name("All files") chooserFilter.add_pattern("*") chooser.add_filter(chooserFilter) chooser.show() response = chooser.run() if response == gtk.RESPONSE_OK: self.filename = chooser.get_filename() if os.path.exists(self.filename): overwrite = confirmDialog(self, "This file already exists. Overwrite this file?") if overwrite == gtk.RESPONSE_YES: self.writeFile() chooser.destroy() return True else: self.filename = None chooser.destroy() return False else: self.writeFile() chooser.destroy() return True else: self.filename = None chooser.destroy() return False
def action_importXML(self, *arg) : filechooserdialog = gtk.FileChooserDialog(_("Import project"), None, \ gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, \ gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) try: filt = gtk.FileFilter() filt.set_name(_("NativeCAM projects")) filt.add_mime_type("text/xml") filt.add_pattern("*.xml") filechooserdialog.add_filter(filt) filt = gtk.FileFilter() filt.set_name(_("All files")) filt.add_pattern("*") filechooserdialog.add_filter(filt) filechooserdialog.set_current_folder(os.path.join(NCAM_DIR, CATALOGS_DIR, self.catalog_dir, PROJECTS_DIR)) filechooserdialog.set_keep_above(True) filechooserdialog.set_transient_for(self.get_toplevel()) if filechooserdialog.run() == gtk.RESPONSE_OK: fname = filechooserdialog.get_filename() try : xml = self.update_features(etree.parse(fname).getroot()) self.import_xml(xml) self.file_changed = True except etree.ParseError as err : mess_dlg(err, _("Import project")) finally: filechooserdialog.destroy() # will update with new features version and keep the previous values
def action_save_project(self, *arg) : global CURRENT_PROJECT filechooserdialog = gtk.FileChooserDialog(_("Save project as..."), None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, \ gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) try: filt = gtk.FileFilter() filt.set_name(_("NativeCAM projects")) filt.add_mime_type("text/xml") filt.add_pattern("*.xml") filechooserdialog.add_filter(filt) d, fname = os.path.split(CURRENT_PROJECT) filechooserdialog.set_current_folder(os.path.join(NCAM_DIR, CATALOGS_DIR, self.catalog_dir, PROJECTS_DIR)) filechooserdialog.set_current_name(fname) filechooserdialog.set_do_overwrite_confirmation(True) filechooserdialog.set_keep_above(True) filechooserdialog.set_transient_for(self.get_toplevel()) if filechooserdialog.run() == gtk.RESPONSE_OK: xml = self.treestore_to_xml() CURRENT_PROJECT = filechooserdialog.get_filename() if CURRENT_PROJECT[-4] != ".xml" not in CURRENT_PROJECT : CURRENT_PROJECT += ".xml" etree.ElementTree(xml).write(CURRENT_PROJECT, pretty_print = True) self.file_changed = False finally: self.display_proj_name() filechooserdialog.destroy()
def on_import_plugin_changed(combobox, widgets, *args): from gtk import FileFilter import plugins.imp plugin_name = widgets['plugin'].get_active_text() __import__("plugins.imp.%s" % plugin_name) ip = eval("plugins.imp.%s.ImportPlugin" % plugin_name) widgets['author'].set_markup("<i>%s</i>" % ip.author) widgets['email'].set_markup("<i>%s</i>" % ip.email) widgets['version'].set_markup("<i>%s</i>" % ip.version) widgets['description'].set_markup("<i>%s</i>" % ip.description) # file filters for i in widgets['fcw'].list_filters(): widgets['fcw'].remove_filter(i) f = FileFilter() f.set_name(plugin_name) if ip.file_filters is not None: if isinstance(ip.file_filters, tuple) or isinstance(ip.file_filters, list): for i in ip.file_filters: f.add_pattern(i) else: f.add_pattern(ip.file_filters) if ip.mime_types is not None: if isinstance(ip.mime_types, tuple) or isinstance(ip.mime_types, list): for i in ip.mime_types: f.add_mime_type(i) else: f.add_mime_type(ip.mime_types) widgets['fcw'].add_filter(f) f = FileFilter() f.set_name(_("All files")) f.add_pattern("*") widgets['fcw'].add_filter(f)
def gui_open_file(self, start_dir=None, types=[]): """Prompt the user to pick a file to open""" import gtk if not start_dir: start_dir = self._last_dir dlg = gtk.FileChooserDialog("Select a file to open", None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_OPEN, gtk.RESPONSE_OK)) if start_dir and os.path.isdir(start_dir): dlg.set_current_folder(start_dir) for desc, spec in types: ff = gtk.FileFilter() ff.set_name(desc) ff.add_pattern(spec) dlg.add_filter(ff) res = dlg.run() fname = dlg.get_filename() dlg.destroy() if res == gtk.RESPONSE_OK: self._last_dir = os.path.dirname(fname) return fname else: return None
def gui_save_file(self, start_dir=None, default_name=None, types=[]): """Prompt the user to pick a filename to save""" import gtk if not start_dir: start_dir = self._last_dir dlg = gtk.FileChooserDialog("Save file as", None, gtk.FILE_CHOOSER_ACTION_SAVE, (gtk.STOCK_CANCEL, gtk.RESPONSE_CANCEL, gtk.STOCK_SAVE, gtk.RESPONSE_OK)) if start_dir and os.path.isdir(start_dir): dlg.set_current_folder(start_dir) if default_name: dlg.set_current_name(default_name) extensions = {} for desc, ext in types: ff = gtk.FileFilter() ff.set_name(desc) ff.add_pattern("*.%s" % ext) extensions[desc] = ext dlg.add_filter(ff) res = dlg.run() fname = dlg.get_filename() ext = extensions[dlg.get_filter().get_name()] if fname and not fname.endswith(".%s" % ext): fname = "%s.%s" % (fname, ext) dlg.destroy() if res == gtk.RESPONSE_OK: self._last_dir = os.path.dirname(fname) return fname else: return None
def action_open_project(self, *arg): global CURRENT_PROJECT if arg[1][0] == 0 : # user project dlg_title = _("Open project") flt_name = _("NativeCAM projects") dir_ = os.path.join(NCAM_DIR, CATALOGS_DIR, self.catalog_dir, PROJECTS_DIR) else : # example dlg_title = _("Open example project") flt_name = _("NativeCAM example projects") dir_ = os.path.join(NCAM_DIR, CATALOGS_DIR, self.catalog_dir, PROJECTS_DIR, EXAMPLES_DIR) filechooserdialog = gtk.FileChooserDialog(dlg_title, None, gtk.FILE_CHOOSER_ACTION_OPEN, (gtk.STOCK_CANCEL, \ gtk.RESPONSE_CANCEL, gtk.STOCK_OK, gtk.RESPONSE_OK)) try: filt = gtk.FileFilter() filt.set_name(flt_name) if arg[1][0] == 0 : filt.add_mime_type("text/xml") filt.add_pattern("*.xml") else : filt.add_pattern("*.*") filechooserdialog.add_filter(filt) filechooserdialog.set_current_folder(dir_) filechooserdialog.set_keep_above(True) filechooserdialog.set_transient_for(self.get_toplevel()) if filechooserdialog.run() == gtk.RESPONSE_OK: filename = filechooserdialog.get_filename() src_data = open(filename).read() if src_data.find(XML_TAG) != 1 : subprocess.call(["xdg-open '%s'" % filename], shell = True) else : xml = etree.fromstring(src_data) xml = self.update_features(xml) self.treestore_from_xml(xml) self.expand_and_select(self.path_to_old_selected) self.clear_undo() CURRENT_PROJECT = filename self.file_changed = False self.action(xml) finally: self.display_proj_name() filechooserdialog.destroy()
def file_chooser(title, action=None, buttons=None, name='', folder=os.path.expanduser('~'), picture=False, backup=False): if mac: if "SAVE" in str(action): if backup: status, filename, path = macutils.saveDialog(['zip']) else: status, filename, path = macutils.saveDialog() else: status, filename, path = macutils.openDialog(['zip']) if status: if filename.lower().endswith('.zip'): pass else: filename = filename+".zip" return filename, path else: return False else: dialog = gtk.FileChooserDialog(title=title, action=action, buttons=buttons) dialog.set_default_response(gtk.RESPONSE_OK) if name: dialog.set_current_name(name) if folder: dialog.set_current_folder(folder) mfilter = gtk.FileFilter() if picture: preview = gtk.Image() dialog.set_preview_widget(preview) dialog.connect("update-preview", update_preview_cb, preview) mfilter.set_name(_("Images")) mfilter.add_mime_type("image/png") mfilter.add_mime_type("image/jpeg") mfilter.add_mime_type("image/gif") mfilter.add_pattern("*.[pP][nN][gG]") mfilter.add_pattern("*.[jJ][pP][eE]?[gG]") mfilter.add_pattern("*.[gG][iI][fF]") mfilter.add_pattern("*.[tT][iI][fF]{1,2}") mfilter.add_pattern("*.[xX][pP][mM]") dialog.add_filter(mfilter) elif backup: mfilter.set_name(_('backups')) mfilter.add_pattern('*.[zZ][iI][pP]') mfilter.add_pattern('*.[gG][rR][iI]') mfilter.add_pattern('*.[dD][bB]') dialog.add_filter(mfilter) mfilter = gtk.FileFilter() mfilter.set_name(_("All files")) mfilter.add_pattern("*") dialog.add_filter(mfilter) response = dialog.run() if response == gtk.RESPONSE_OK: filename = dialog.get_filename() elif response == gtk.RESPONSE_CANCEL: filename = None else: return False path = dialog.get_current_folder() dialog.destroy() return filename, path