def import_task(self, filepath=None, with_directory=False): """ Import task file to project Qt5 change: https://www.reddit.com/r/learnpython/comments/2xhagb/pyqt5_trouble_with_openinggetting_the_name_of_the/ :param filepath: :return: """ if self.path is None or len(self.path) == 0 or not self.is_saved(): self.warning( 'To import a protocol you need to save the project first.', 'Project not saved yet') else: if not filepath: filepath, _ = QFileDialog.getOpenFileName(self, 'OpenFile') if filepath: try: return super(ProjectTreeNode, self).import_task(filepath, with_directory) except Exception as e: self.warning(str(e), 'Import aborted') return None
def main(argv=sys.argv): if len(argv) == 1: filename = None else: if argv[1] in ["-h", "--help"]: usage(argv) return 0 else: filename = argv[1] app = QApplication(argv) app.setAttribute(Qt.AA_EnableHighDpiScaling) app.setAttribute(Qt.AA_UseHighDpiPixmaps) if filename is None: filename, _ = QFileDialog.getOpenFileName( None, "Image file", os.path.expanduser("~/Documents"), "Image (*.png)") if not filename: return 1 print(filename) form = MainForm(filename=filename) rect = QApplication.desktop().availableGeometry() form.show() form.raise_() return app.exec_()
def open_report(self): filename, _ = QFileDialog.getOpenFileName( self, "Open Report", self.open_dir, "Report (*.report)") if not filename: return self.report_changed = False self.open_dir = os.path.dirname(filename) self.saveSettings() try: report = self.load(filename) except (IOError, AttributeError, pickle.UnpicklingError) as e: message_critical( self.tr("Could not load an Orange Report file"), title=self.tr("Error"), informative_text=self.tr("Error occurred " "while loading '{}'.").format(filename), exc_info=True, parent=self) log.error(str(e), exc_info=True) return self.set_instance(report) self = report self._build_html() self.table.selectRow(0) self.show() self.raise_()
def browseNetFile(self, browse_demos=False): """user pressed the '...' button to manually select a file to load""" if browse_demos: from pkg_resources import load_entry_point startfile = next(load_entry_point("Orange3-Network", "orange.data.io.search_paths", "network")())[1] else: startfile = self.recentFiles[0] if self.recentFiles else '.' filename, _ = QFileDialog.getOpenFileName( self, 'Open a Network File', startfile, ';;'.join(("All network files (*{})".format( ' *'.join(network.readwrite.SUPPORTED_READ_EXTENSIONS)), "NetworkX graph as Python pickle (*.gpickle)", "NetworkX edge list (*.edgelist)", "Pajek files (*.net *.pajek)", "GML files (*.gml)", "All files (*)"))) if not filename: return try: self.recentFiles.remove(filename) except ValueError: pass self.recentFiles.insert(0, filename) self.populate_comboboxes() self.net_index = 0 self.selectNetFile()
def open_report(self): filename, _ = QFileDialog.getOpenFileName(self, "Open Report", self.open_dir, "Report (*.report)") if not filename: return self.report_changed = False self.open_dir = os.path.dirname(filename) self.saveSettings() try: report = self.load(filename) except (IOError, AttributeError, pickle.UnpicklingError) as e: message_critical(self.tr("Could not load an Orange Report file"), title=self.tr("Error"), informative_text=self.tr( "Error occurred " "while loading '{}'.").format(filename), exc_info=True, parent=self) log.error(str(e), exc_info=True) return self.set_instance(report) self = report self._build_html() self.table.selectRow(0) self.show() self.raise_()
def get_msh_pth_and_set_to_ctrl(self) -> str: """ Gets msh path from dialog and sets it to control holding msh path Returns ------- str msh path """ msh_pth, _ = \ QFileDialog.getOpenFileName( self.parent, self.label, self.value) # print('msh_pth') # print(msh_pth) # print(self.msh_fname_tab_key) # print(self.msh_fname_param_key) # print(self.param_ctrl_key) des_msh_pth_ctrl = \ self.su2_cfg_obj.parsed_su2_cfg[self.msh_fname_tab_key]\ [self.msh_fname_param_key][self.param_ctrl_key] # getting only the relaltive piece of the path for SU2 hangs otherwise semi_rel_pth = msh_pth.split(os.sep)[-1] print('### semi_rel_pth ###') print(semi_rel_pth) # des_msh_pth_ctrl.value = msh_pth des_msh_pth_ctrl.value = semi_rel_pth # des_msh_pth_ctrl.form.lineEdit.setText(msh_pth) return msh_pth
def browse_file(self): start_file = self.last_path() or stdpaths.Documents filename, _ = QFileDialog.getOpenFileName(self, 'Open Distance File', start_file, self.FILTER) if not filename: return self.add_path(filename) self.open_file()
def click(self): value = QFileDialog.getOpenFileName(self.parent, self._label, self.value, self.filter) if conf.PYFORMS_USE_QT5: value = value[0] else: value = str(value) if value and len(value)>0: self.value = value
def click(self): if self.opened_file_type == 'txt': value, _ = QFileDialog.getOpenFileName( self.parent, self._label, self.value, "Text Files (*.txt);;All Files (*)") elif self.opened_file_type == 'xlsx': value, _ = QFileDialog.getOpenFileName( self.parent, self._label, self.value, "Excel Files (*.xlsx);;All Files (*)") elif self.opened_file_type == 'json': value, _ = QFileDialog.getOpenFileName( self.parent, self._label, self.value, "Config Files (*.json);;All Files (*)") else: value, _ = QFileDialog.getOpenFileName(self.parent, self._label, self.value) if value and len(value) > 0: self.value = value
def browse(self): start_file = self.last_path() or os.path.expanduser("~/") formats = ["Text files (*.txt)", "All files (*)"] file_name, _ = QFileDialog.getOpenFileName( None, "Open...", start_file, ";;".join(formats), formats[0]) if not file_name: return self.add_path(file_name) self._activate()
def read_from_file(parent: OWWidget) -> Optional[Ontology]: filename, _ = QFileDialog.getOpenFileName( parent, "Open Ontology", os.path.expanduser("~/"), ";;".join(LoadFileFormats) ) if not filename: return None return _read_from_source(filename)
def onAddScriptFromFile(self, *_): filename, _ = QFileDialog.getOpenFileName( self, 'Open Python Script', os.path.expanduser("~/"), 'Python files (*.py)\nAll files(*.*)') if filename: name = os.path.basename(filename) with tokenize.open(filename) as f: contents = f.read() self.libraryList.append(Script(name, contents, 0, filename)) self.setSelectedScript(len(self.libraryList) - 1)
def onAddScriptFromFile(self, *args): filename, _ = QFileDialog.getOpenFileName( self, '打开Python脚本', os.path.expanduser("~/"), 'Python files (*.py)\nAll files(*.*)') if filename: name = os.path.basename(filename) # TODO: use `tokenize.detect_encoding` with open(filename, encoding="utf-8") as f: contents = f.read() self.libraryList.append(Script(name, contents, 0, filename)) self.setSelectedScript(len(self.libraryList) - 1)
def browse_file(self): start_file = self.last_path() or os.path.expanduser("~/") dialog_formats = "VCF files (*.vcf);;All files (*)" filename, _ = QFileDialog.getOpenFileName(self, 'Open Orange Data File', start_file, dialog_formats) if not filename: return self.add_path(filename) self.load_data()
def onAddScriptFromFile(self, *args): filename, _ = QFileDialog.getOpenFileName( self, 'Open Python Script', os.path.expanduser("~/"), 'Python files (*.py)\nAll files(*.*)' ) if filename: name = os.path.basename(filename) contents = open(filename, "rb").read().decode("utf-8", errors="ignore") self.libraryList.append(Script(name, contents, 0, filename)) self.setSelectedScript(len(self.libraryList) - 1)
def import_clicked(self): filename = str(QFileDialog.getOpenFileName(self, 'Choose a file', '')) if filename != "": infile = open(filename, 'r') polygons = [] for line in infile: values = line.split(';') name = values[0] poly = values[1] polygons.append((name, poly)) self._polygons.value += polygons
def __on_import_word_list(self): filename, _ = QFileDialog.getOpenFileName( self, "Open Word List", os.path.expanduser("~/"), "Text files (*.txt)\nAll files(*.*)") if filename: name = os.path.basename(filename) with open(filename, encoding="utf-8") as f: words = [line.strip() for line in f.readlines()] self.library_model.append(WordList(name, words, filename=filename)) self._set_selected_word_list(len(self.library_model) - 1) self._apply_update_rule()
def browse(self): """Select a filename using an open file dialog.""" if self.filename is None: startdir = stdpaths.Documents else: startdir = os.path.dirname(self.filename) filename, _ = QFileDialog.getOpenFileName( self, self.tr("Open"), directory=startdir, filter=self.FILTER) if filename: self.load(filename)
def onAddScriptFromFile(self, *args): filename, _ = QFileDialog.getOpenFileName( self, 'Open Python Script', os.path.expanduser("~/"), 'Python files (*.py)\nAll files(*.*)' ) if filename: name = os.path.basename(filename) # TODO: use `tokenize.detect_encoding` with open(filename, encoding="utf-8") as f: contents = f.read() self.libraryList.append(Script(name, contents, 0, filename)) self.setSelectedScript(len(self.libraryList) - 1)
def browse_file(self): """ Open the file dialog and open the chosen file. """ start_path = self.last_path() or os.path.expanduser("~") filename, _ = QFileDialog.getOpenFileName( None, "Open File", start_path, 'All Files (*.*)') if not filename: return self.closeContext() self.add_path(filename) self.openContext(filename) self.open_file()
def click(self): if self.use_save_dialog: value, _ = QFileDialog.getSaveFileName(self.parent, self._label, self.value) else: if conf.PYFORMS_DIALOGS_OPTIONS: value = QFileDialog.getOpenFileName( self.parent, self._label, self.value, options=conf.PYFORMS_DIALOGS_OPTIONS) else: value = QFileDialog.getOpenFileName(self.parent, self._label, self.value) if _api.USED_API == _api.QT_API_PYQT5: value = value[0] elif _api.USED_API == _api.QT_API_PYQT4: value = str(value) if value and len(value) > 0: self.value = value self._value_before_finishEditing = value
def click(self): if self.use_save_dialog: value = QFileDialog.getSaveFileName(self.parent, self._label, self.value) else: value = QFileDialog.getOpenFileName(self.parent, self._label, self.value) if conf.PYFORMS_USE_QT5: value = value[0] else: value = str(value) if value and len(value) > 0: self.value = value
def click(self): if self.use_save_dialog: value = QFileDialog.getSaveFileName(self.parent, self._label, self.value) else: value = QFileDialog.getOpenFileName(self.parent, self._label, self.value) if _api.USED_API == _api.QT_API_PYQT5: value = value[0] elif _api.USED_API == _api.QT_API_PYQT4: value = str(value) if value and len(value) > 0: self.value = value
def load(self): fname, _ = QFileDialog.getOpenFileName( self, "File name", self._start_dir(), "Variable definitions (*.colors)") if not fname: return try: with open(fname) as f: js = json.load(f) #: dict self._parse_var_defs(js) except IOError: QMessageBox.critical(self, "File error", "File cannot be opened.") return except (json.JSONDecodeError, InvalidFileFormat): QMessageBox.critical(self, "File error", "Invalid file format.") return
def browse_file(self, in_demos=False): if in_demos: start_file = get_sample_datasets_dir() if not os.path.exists(start_file): QMessageBox.information( None, "File", "Cannot find the directory with documentation datasets") return else: start_file = self.last_path() or os.path.expanduser("~/") filename, _ = QFileDialog.getOpenFileName( self, 'Open Distance File', start_file, "(*.dst)") if not filename: return self.add_path(filename) self.open_file()
def browse_file(self, in_demos=False): if in_demos: start_file = get_sample_datasets_dir() if not os.path.exists(start_file): QMessageBox.information( None, "File", "Cannot find the directory with documentation data sets") return else: start_file = self.last_path() or os.path.expanduser("~/") filename, _ = QFileDialog.getOpenFileName( self, 'Open Distance File', start_file, "(*.dst)") if not filename: return self.add_path(filename) self.open_file()
def browse_file(self, in_demos=False): if in_demos: start_file = get_sample_datasets_dir() if not os.path.exists(start_file): QMessageBox.information( None, "File", "Cannot find the directory with documentation data sets") return else: start_file = self.last_path() or os.path.expanduser("~/") filename, _ = QFileDialog.getOpenFileName( self, 'Open Orange Data File', start_file, dialog_formats()) if not filename: return self.add_path(filename) self.source = self.LOCAL_FILE self.load_data()
def browse_file(self, in_demos=False): if in_demos: start_file = get_sample_datasets_dir() if not os.path.exists(start_file): QMessageBox.information( None, "File", "Cannot find the directory with documentation data sets") return else: start_file = self.last_path() or os.path.expanduser("~/") filename, _ = QFileDialog.getOpenFileName(self, 'Open Orange Data File', start_file, self.dlg_formats) if not filename: return self.add_path(filename) self.source = self.LOCAL_FILE self.load_data()
def browse_net_file(self, browse_demos=False): """user pressed the '...' button to manually select a file to load""" if browse_demos: startfile = demos_path else: startfile = self.recentFiles[0] if self.recentFiles else '.' filename, _ = QFileDialog.getOpenFileName( self, 'Open a Network File', startfile, ';;'.join(("Pajek files (*.net *.pajek)",))) if not filename: return if filename in self.recentFiles: self.recentFiles.remove(filename) self.recentFiles.insert(0, filename) self.populate_comboboxes() self.net_index = 0 self.select_net_file()
def __prompt_read_file_evt(self): """ Opens a window for user to select where to save the sound .bin file """ self._filename.value, _ = QFileDialog.getOpenFileName(caption='Choose sound file', filter='Harp sound file(*.bin);;WAV(*.wav)') if self._filename.value: # assume that if the file extension ends .wav is a wave file, otherwise just try to read as a Harp sound if self._filename.value.endswith('.wav'): try: fs, data = wavfile.read(self._filename.value) except Exception: self.critical("Error while opening the WAV file. Please try again or try loading another file.") return self._wave_int = np.array(data, dtype=np.int32) else: self._wave_int = np.fromfile(self._filename.value, dtype=np.int32) self.sound_loaded() self._loaded = True
def browseDataFile(self): if self.graph is None: self.Warning.no_network_file() return startfile = (self.recentDataFiles[0] if self.recentDataFiles else path.dirname(self.recentFiles[0]) if self.recentFiles else '.') filename = QFileDialog.getOpenFileName( self, 'Open a Vertices Data File', startfile, 'Data files (*.tab *.tsv *.csv);;' 'All files(*)') if not filename: return try: self.recentDataFiles.remove(filename) except ValueError: pass self.recentDataFiles.insert(0, filename) self.populate_comboboxes() self.data_index = 0 self.selectDataFile()
def _file_oper_click(self): value = None if self.use_save_dialog is True: # QFileDialog.getSaveFileName returns (path, AllFiles(*)) value, _ = \ QFileDialog.getSaveFileName( self.parent, self.label, self.value) elif self.use_save_dialog is False: # QFileDialog.getOpenFileName returns (path, AllFiles(*)) value, _ = \ QFileDialog.getOpenFileName( self.parent, self.label, self.value) else: raise ValueError( """Unsupported value for 'use_save_dialog' parameter""") value = str(value) # print(value) # TODO fix when no load file is provided if value: if self.use_save_dialog is True: self.su2_config.save_path = value self.trgt_cfg_editor.saved_config_path_label = \ self.su2_config.save_path saver = SU2ConfigSaver(su2_cfg_obj=self.su2_config, vert_tabs_w=self.trgt_tabs_w) saver.write_cfg() if self.use_save_dialog is False: self.su2_config.load_path = value self._parse_cfg(tb_prsd_cfg_pth=value) self.su2_config.set_sections_labels() self.trgt_tabs_w.vert_tab_w_ctrl._set_tabs() self.trgt_sect_sel_w.chkbx_list_ctrl._set_sections() # tabs must be refreshed self.trgt_cfg_editor.loaded_config_path_label = \ self.su2_config.load_path
def __handle_file_selector(self): self.file_path = QFileDialog.getOpenFileName(self, 'Open File')[0] self.file_info.setText('Selected File: {}'.format( os.path.basename(self.file_path)))
def addDoc(self): self.y.lineEdit_28.clear() fname, _ = QFileDialog.getOpenFileName(self, 'Open file', 'c:\\', "Image files (*.pdf)") self.fname = fname self.y.lineEdit_28.insert(os.path.realpath(self.fname))