tkinter:如何让用户在 asksaveasfilename-dialog 中选择文件类型? [英] tkinter: How can I get the users choice of filetype in the asksaveasfilename-dialog?

查看:55
本文介绍了tkinter:如何让用户在 asksaveasfilename-dialog 中选择文件类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在编写一个 GUI,它应该允许用户保存文件并选择文件类型(比如:jpg、bmp).我在 Python 3.4 中使用 tkinter.

currently, I'm programming a GUI, which should allow the user to save a file and select the filetype (lets say: jpg, bmp). I'm using tkinter with Python 3.4.

不幸的是,我无法弄清楚用户选择了哪种文件类型:我正在使用 asksaveasfilename-dialog,我只能得到路径:

Unfortunately, I am unable figure out, which filetype the user has selected: I'm using the asksaveasfilename-dialog and all I can get is the path:

from tkinter import filedialog
path = filedialog.asksaveasfilename(filetypes = [('Bitmap', '.bmp'),('jpg', '.jpg')])

我可以尝试从路径中提取扩展名,但这可能会导致意外行为:

I can try to extract the extension from the path, but that can leed to unexpected behaviour:

  1. 如果用户未在文件名中提供扩展名但选择了文件类型,则路径"不包含任何扩展名
  2. 如果用户首先决定使用文件名example.bmp",但随后选择 jpg 作为文件类型,则扩展名不会更新(因为它通常在窗口中).所以路径"仍然是:C:\ ... \example.bmp

有没有办法获取用户选择的文件类型?还有其他办法吗?

Is there a way to get the filetype, that the user selected? Is there any other way around?

推荐答案

当我遇到自动更新扩展的同样问题时,我偶然发现了您的问题.显然你可以用插件来试试:

As I struggle with the same problem of automatically updating the extension, I stumbled over your question. Apparently you can try that with addins:

pythonaddins.SaveDialog = saveFileDialog

这与 OpenDialog 几乎相同,除了我们可以自动将特定扩展名附加到用户输入中,而不是过滤文件.也可以传入文件类型描述和扩展名的元组列表,然后填充另存为文件类型下拉列表,但同样唯一要做的就是过滤显示的文件.实际上是 FileExtension 参数将扩展名附加到返回值.

This one is almost identical to OpenDialog except that instead of filtering files, we have the ability to automatically append a particular extension to the user's input. It's also possible to pass in a tupled list of filetype descriptions and extension which would then populate the Save As Filetype Dropdown, but again the only thing that is doing is filtering the files displayed. It's actually the FileExtension parameter that is appending the extension to the return value.

这篇关于tkinter:如何让用户在 asksaveasfilename-dialog 中选择文件类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆