本文整理汇总了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()
示例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
示例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()
示例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()
示例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
示例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