當前位置: 首頁>>代碼示例>>Python>>正文


Python QFileDialog.getSaveFileName方法代碼示例

本文整理匯總了Python中AnyQt.QtWidgets.QFileDialog.getSaveFileName方法的典型用法代碼示例。如果您正苦於以下問題:Python QFileDialog.getSaveFileName方法的具體用法?Python QFileDialog.getSaveFileName怎麽用?Python QFileDialog.getSaveFileName使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在AnyQt.QtWidgets.QFileDialog的用法示例。


在下文中一共展示了QFileDialog.getSaveFileName方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: saveScript

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
    def saveScript(self):
        index = self.selectedScriptIndex()
        if index is not None:
            script = self.libraryList[index]
            filename = script.filename
        else:
            filename = os.path.expanduser("~/")

        filename, _ = QFileDialog.getSaveFileName(
            self, 'Save Python Script',
            filename,
            'Python files (*.py)\nAll files(*.*)'
        )

        if filename:
            fn = ""
            head, tail = os.path.splitext(filename)
            if not tail:
                fn = head + ".py"
            else:
                fn = filename

            f = open(fn, 'w')
            f.write(self.text.toPlainText())
            f.close()
開發者ID:acopar,項目名稱:orange3,代碼行數:27,代碼來源:owpythonscript.py

示例2: save_report

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
    def save_report(self):
        """Save report"""
        filename, _ = QFileDialog.getSaveFileName(
            self, "Save Report", self.save_dir,
            "HTML (*.html);;PDF (*.pdf);;Report (*.report)")
        if not filename:
            return QDialog.Rejected

        self.save_dir = os.path.dirname(filename)
        self.saveSettings()
        _, extension = os.path.splitext(filename)
        if extension == ".pdf":
            printer = QPrinter()
            printer.setPageSize(QPrinter.A4)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(filename)
            self.report_view.print_(printer)
        elif extension == ".report":
            self.save(filename)
        else:
            def save_html(contents):
                try:
                    with open(filename, "w", encoding="utf-8") as f:
                        f.write(contents)
                except PermissionError:
                    self.permission_error(filename)

            save_html(self.report_view.html())
        self.report_changed = False
        return QDialog.Accepted
開發者ID:benzei,項目名稱:orange3,代碼行數:32,代碼來源:owreport.py

示例3: save_image_as

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
    def save_image_as(self):
        img = self.get_selection().toImage()
        if img.isNull():
            QMessageBox.critical(self, self.tr('Error'), self.tr('No image was selected!'))
            return

        self.hide()

        formats = {
            self.tr('Portable Network Graphics (*.png)'): 'png',
            self.tr('Joint Photographic Experts Group (*.jpg *.jpeg)'): 'jpg',
            self.tr('Graphics Interchange Format (*.gif)'): 'gif',
            self.tr('Bitmap (*.bmp)'): 'bmp',
            self.tr('All Images (*.png *.jpg *.gif *.bmp)'): 'all'
        }

        file_format = None
        destination = QFileDialog.getSaveFileName(self, 'Save image', '', ';;'.join(formats.keys()))
        if isinstance(destination, tuple):
            destination, file_format = destination
            file_format = formats[file_format]
            if file_format == 'all':
                file_format = None

        if not file_format:
            file_format = destination.rsplit('.', 1)[-1]

        if destination:
            if file_format not in formats.values():
                file_format = 'png'
            if not destination.endswith('.' + file_format):
                destination += '.' + file_format
            img.save(destination, file_format, 0 if file_format == 'png' else 90)
        self.reject()
開發者ID:rokups,項目名稱:paste2box,代碼行數:36,代碼來源:screenshot.py

示例4: save_file_as

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
 def save_file_as(self):
     file_name = self.filename or self.last_dir or os.path.expanduser("~")
     filename, _ = QFileDialog.getSaveFileName(
         self, "Select file", file_name, 'Distance files (*.dst)')
     if not filename:
         return
     self.filename = filename
     self.unconditional_save_file()
     self.last_dir = os.path.split(self.filename)[0]
     self.adjust_label()
開發者ID:PrimozGodec,項目名稱:orange3,代碼行數:12,代碼來源:owsavedistances.py

示例5: get_file_name

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
def get_file_name(start_dir, start_filter, file_formats):
    """
    Get filename for the given possible file formats

    The function uses the standard save file dialog with filters from the
    given file formats. Extension is added automatically, if missing. If the
    user enters file extension that does not match the file format, (s)he is
    given a dialog to decide whether to fix the extension or the format.

    Function also returns the writer and filter to cover the case where the
    same extension appears in multiple filters. Although `file_format` is a
    dictionary that associates its extension with one writer, writers can
    still have other extensions that are allowed.

    Args:
        start_dir (str): initial directory, optionally including the filename
        start_filter (str): initial filter
        file_formats (dict {extension: Orange.data.io.FileFormat}): file formats
    Returns:
        (filename, writer, filter), or `(None, None, None)` on cancel
    """
    writers = sorted(set(file_formats.values()), key=lambda w: w.PRIORITY)
    filters = [format_filter(w) for w in writers]
    if start_filter not in filters:
        start_filter = filters[0]

    while True:
        filename, filter = QFileDialog.getSaveFileName(
            None, 'Save As...', start_dir, ';;'.join(filters), start_filter)
        if not filename:
            return None, None, None

        writer = writers[filters.index(filter)]
        base, ext = os.path.splitext(filename)
        if not ext:
            filename += writer.EXTENSIONS[0]
        elif ext not in writer.EXTENSIONS:
            format = writer.DESCRIPTION
            suggested_ext = writer.EXTENSIONS[0]
            suggested_format = \
                ext in file_formats and file_formats[ext].DESCRIPTION
            res = fix_extension(ext, format, suggested_ext, suggested_format)
            if res == fix_extension.CANCEL:
                continue
            if res == fix_extension.CHANGE_EXT:
                filename = base + suggested_ext
            elif res == fix_extension.CHANGE_FORMAT:
                writer = file_formats[ext]
                filter = format_filter(writer)
        return filename, writer, filter
開發者ID:kernc,項目名稱:orange3,代碼行數:52,代碼來源:filedialogs.py

示例6: save_report

# 需要導入模塊: from AnyQt.QtWidgets import QFileDialog [as 別名]
# 或者: from AnyQt.QtWidgets.QFileDialog import getSaveFileName [as 別名]
    def save_report(self):
        """Save report"""
        formats = OrderedDict((('HTML (*.html)', '.html'),
                               ('PDF (*.pdf)', '.pdf'),
                               ('Report (*.report)', '.report')))

        filename, selected_format = QFileDialog.getSaveFileName(
            self, "Save Report", self.save_dir, ';;'.join(formats.keys()))
        if not filename:
            return QDialog.Rejected

        # Set appropriate extension if not set by the user
        expect_ext = formats[selected_format]
        if not filename.endswith(expect_ext):
            filename += expect_ext

        self.save_dir = os.path.dirname(filename)
        self.saveSettings()
        _, extension = os.path.splitext(filename)
        if extension == ".pdf":
            printer = QPrinter()
            printer.setPageSize(QPrinter.A4)
            printer.setOutputFormat(QPrinter.PdfFormat)
            printer.setOutputFileName(filename)
            self._print_to_printer(printer)
        elif extension == ".report":
            self.save(filename)
        else:
            def save_html(contents):
                try:
                    with open(filename, "w", encoding="utf-8") as f:
                        f.write(contents)
                except PermissionError:
                    self.permission_error(filename)

            save_html(self.report_view.html())
        self.report_changed = False
        return QDialog.Accepted
開發者ID:astaric,項目名稱:orange3,代碼行數:40,代碼來源:owreport.py


注:本文中的AnyQt.QtWidgets.QFileDialog.getSaveFileName方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。