tkFileDialog 未将结果转换为 Windows 上的 Python 列表
我使用下面的代码(Python 2.7 和 Python 3.2)来显示支持多项选择的“打开文件”对话框。在 Linux 上,文件名是一个 python 列表,但在 Windows 上,文件名返回为 {C:/Documents and Settings/IE User/My Documents/VPC_EULA.txt} {C:/Documents and Settings/IE User/My Documents/ VPC_ReadMe.txt}
,即原始 TCL 列表。
这是一个 python 错误吗?这里有人知道将原始 TCL 列表转换为 python 列表的好方法吗?
if sys.hexversion >= 0x030000F0:
import tkinter.filedialog as filedialog
else:
import tkFileDialog as filedialog
options = {}
options['filetypes'] = [('vnote files', '.vnt') ,('all files', '.*')]
options['multiple'] = 1
filenames = filedialog.askopenfilename(**options)
I'm using the code below (Python 2.7 and Python 3.2) to show an Open Files dialog that supports multiple-selection. On Linux filenames is a python list, but on Windows filenames is returned as {C:/Documents and Settings/IE User/My Documents/VPC_EULA.txt} {C:/Documents and Settings/IE User/My Documents/VPC_ReadMe.txt}
, i.e. a raw TCL list.
Is this a python bug, and does anyone here know a good way to convert the raw TCL list into a python list?
if sys.hexversion >= 0x030000F0:
import tkinter.filedialog as filedialog
else:
import tkFileDialog as filedialog
options = {}
options['filetypes'] = [('vnote files', '.vnt') ,('all files', '.*')]
options['multiple'] = 1
filenames = filedialog.askopenfilename(**options)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
问题在于 Tcl、Tk 和 Python 之间的“有趣”交互,其中每一个都在自己做一些明智的事情,但组合的行为却并不正确。深层问题是 Tcl 和 Python 对于类型含义有非常不同的想法,这表现为 Tcl 将其视为列表但 Python 将其视为字符串的值(Tk 中的代码)假设Python不需要小心保持干净)。可以说,Python 接口应该利用这样一个事实:它可以知道 Tcl 列表将从多重选择中返回并隐藏它,但事实并非如此,因此您会陷入困境。
我可以(而且应该!)在 Tk 中修复此问题,但我不知道该修复需要多长时间才能以这种方式返回给您。
[编辑]:此问题现已修复(使用 此 补丁)在 Tk 8.5 维护分支和主要开发分支上。我无法预测您何时能够获得固定版本,除非您从我们的化石存储库中获取源代码并自己构建它。
The problem is an “interesting” interaction between Tcl, Tk and Python, each of which is doing something sensible on its own but where the combination isn't behaving correctly. The deep issue is that Tcl and Python have very different ideas about what types mean, and this is manifesting itself as a value that Tcl sees as a list but Python sees as a string (with the code in Tk assuming that it doesn't need to be careful to be clean for Python). Arguably the Python interface should use the fact that it can know that a Tcl list will be coming back from a multiple selection and hide this, but it doesn't so you're stuck.
I can (and should!) fix this in Tk, but I don't know how long it would take for the fix to find its way back to you that way.
[EDIT]: This is now fixed (with this patch) in the Tk 8.5 maintenance branch and on the main development branch. I can't predict when you'll be able to get a fixed version unless you grab the source out of our fossil repository and build it yourself.
由于某种原因,基于 tk_eval 的修复对我不起作用。 tkFileDialog 返回的字符串中的文件名仅在包含空格时才包含在 {} 括号中,而 tcl 文档似乎暗示所有列表项都应由这些括号分隔。
不管怎样,这里有一个似乎对我有用的修复(Windows 7 上的 python 2.7.3):
For some reason the tk_eval based fix doesn't work for me. The filenames in the string returned by tkFileDialog are only wrapped in {} brackets if they contain whitespace, whereas the tcl docs seem to imply that all list items should be delimited by those brackets.
Anyway, here's a fix that seems to work for me (python 2.7.3 on windows 7):
这个修复对我有用:
This fix works for me:
我用过的一个快速方法:
A quick way that I've used: