Python 如何从tkinterDND2事件对象正确获取文件路径
我正在尝试使用python中的tkinter在一个简单的GUI应用程序中实现“在此处放置文件”功能 它使用TkinterDnD2,最后看到这个 在删除文件event.data时,返回用大括号括起来的文件名 示例:Python 如何从tkinterDND2事件对象正确获取文件路径,python,user-interface,tkinter,Python,User Interface,Tkinter,我正在尝试使用python中的tkinter在一个简单的GUI应用程序中实现“在此处放置文件”功能 它使用TkinterDnD2,最后看到这个 在删除文件event.data时,返回用大括号括起来的文件名 示例:{d:/test/sample1.pdf} 但是,当在目标中放置多个文件时,每个路径都被大括号包围 示例:{D:/test/sample1.pdf}{D:/test/sample2.pdf} 如何正确解析此字符串中的文件路径?我不确定TkinterDnD2库是返回单个字符串中的路径流还是
{d:/test/sample1.pdf}
但是,当在目标中放置多个文件时,每个路径都被大括号包围
示例:{D:/test/sample1.pdf}{D:/test/sample2.pdf}
如何正确解析此字符串中的文件路径?我不确定TkinterDnD2库是返回单个字符串中的路径流还是返回单个字符串中的路径流 如果是字符串中的单路径,则解决方案很简单:
>>> str1="{mypath/to/file}"
>>> str1[1:-1]
输出将是
'mypath/to/file'
但我想你的问题是多条路径以一个字符串的形式出现。您可以实现如下代码
将str1视为具有多个路径的字符串:
str1="{mypath1/tofile1}{mypath3/tofile3}{mypath2/tofile2}"
i=0
patharr=[]
while(str1.find('{',i)>=0):
strtIndex=str1.find('{',i)
if(str1.find('}',strtIndex+1)>0):
endIndex=str1.find('}',strtIndex+1)
patharr.append(str1[strtIndex+1:endIndex])
print(str1[strtIndex:endIndex+1])
i=endIndex+1
print(patharr)
现在输出如下:
['mypath1/tofile1'、'mypath3/tofile3'、'mypath2/tofile2']
因此,根据您在下面评论中的问题,是文件路径中带有花括号的文件路径代码
str1 = "{mypath}}}}1/tofil{{{e1}{mypath3/tofile3}{mypath2/tofile2}}}}}}"
i = 0
patharr = []
while (str1.find('{', i) >= 0):
strtIndexfirst = str1.find('{', i)
notFound = True
strtIndex = strtIndexfirst
while (notFound and strtIndex < len(str1)):
if (str1.find('}', strtIndex + 1) > 0):
findInd = str1.find('}', strtIndex + 1)
if (findInd == len(str1) - 1):
endIndex = str1.find('}', strtIndex + 1)
patharr.append(str1[strtIndexfirst + 1:endIndex])
print(str1[strtIndex:endIndex + 1])
i = endIndex + 1
break
if (str1[findInd + 1] == '{'):
endIndex = str1.find('}', strtIndex + 1)
patharr.append(str1[strtIndexfirst + 1:endIndex])
print(str1[strtIndex:endIndex + 1])
i = endIndex + 1
notFound = False
else:
strtIndex = findInd
else:
strtIndex = str1.find('}', strtIndex + 1)
print(patharr)
输出为
['{mypath1/tofil}}e1',mypath3/tofile3',mypath2/tofile2}']
也适用于其他测试用例。这段代码工作正常。我不确定TkinterDnD2库是返回单个字符串中的路径流,还是返回字符串中的单个路径 如果是字符串中的单路径,则解决方案很简单:
>>> str1="{mypath/to/file}"
>>> str1[1:-1]
输出将是
'mypath/to/file'
但我想你的问题是多条路径以一个字符串的形式出现。您可以实现如下代码
将str1视为具有多个路径的字符串:
str1="{mypath1/tofile1}{mypath3/tofile3}{mypath2/tofile2}"
i=0
patharr=[]
while(str1.find('{',i)>=0):
strtIndex=str1.find('{',i)
if(str1.find('}',strtIndex+1)>0):
endIndex=str1.find('}',strtIndex+1)
patharr.append(str1[strtIndex+1:endIndex])
print(str1[strtIndex:endIndex+1])
i=endIndex+1
print(patharr)
现在输出如下:
['mypath1/tofile1'、'mypath3/tofile3'、'mypath2/tofile2']
因此,根据您在下面评论中的问题,是文件路径中带有花括号的文件路径代码
str1 = "{mypath}}}}1/tofil{{{e1}{mypath3/tofile3}{mypath2/tofile2}}}}}}"
i = 0
patharr = []
while (str1.find('{', i) >= 0):
strtIndexfirst = str1.find('{', i)
notFound = True
strtIndex = strtIndexfirst
while (notFound and strtIndex < len(str1)):
if (str1.find('}', strtIndex + 1) > 0):
findInd = str1.find('}', strtIndex + 1)
if (findInd == len(str1) - 1):
endIndex = str1.find('}', strtIndex + 1)
patharr.append(str1[strtIndexfirst + 1:endIndex])
print(str1[strtIndex:endIndex + 1])
i = endIndex + 1
break
if (str1[findInd + 1] == '{'):
endIndex = str1.find('}', strtIndex + 1)
patharr.append(str1[strtIndexfirst + 1:endIndex])
print(str1[strtIndex:endIndex + 1])
i = endIndex + 1
notFound = False
else:
strtIndex = findInd
else:
strtIndex = str1.find('}', strtIndex + 1)
print(patharr)
输出为
['{mypath1/tofil}}e1',mypath3/tofile3',mypath2/tofile2}']
也适用于其他测试用例。这段代码运行良好。使用这两行python代码可以轻松实现这一点:
temp = filePathsReturned[1:-1]
list = temp.split("} {")
这可以通过以下两行python代码轻松实现:
temp = filePathsReturned[1:-1]
list = temp.split("} {")
张贴你写的代码。否则,首先对其进行编码并使用您的代码编辑帖子。示例是event.data返回的字符串。。那就是。。每条路径都被花括号包围。。我只想从该字符串中提取路径..发布您编写的代码。否则,首先对其进行编码并使用您的代码编辑帖子。示例是event.data返回的字符串。。那就是。。每条路径都被花括号包围。。我只想从该字符串中提取路径..我想过这样做..但如果文件名中有大括号,则可能会出现问题..@DivakarRajesh请检查现在编辑和更新的答案这对
{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/sample2.PDF}{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/test.PDF}{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/sample1.PDF}
path end}和path start之间存在差距{。这不是预期的。这又是大量的工作。以后可能会对您有所帮助。非常感谢您的努力。我自己已经解决了它。我曾想过做类似的事情。但是如果文件名中有大括号,这可能会成为一个问题。@DivakarRajesh请检查现在编辑和更新的答案这不适用于{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/sample2.PDF}{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/test.PDF}{C:/Users/DivakarRajesh/Desktop/Coders/Basic Programming Concepts using C/utility programs in python/Working with PDF/sample1.PDF}
path end}和path start之间存在差距{。这不是预期的。这又是大量的工作。以后可能会对您有所帮助。非常感谢您的努力。我自己已经解决了。是的,很好。请继续。如果路径之间没有间隙,并且存在多个{},我的答案很好在我的回答中突出显示的花括号实际上这个答案只是从你的答案中衍生出来的。非常感谢你提供的见解。是的,这很好。如果路径之间没有间隙,并且有多个{},我的答案很好用我的回答中突出显示的花括号实际上这个答案只是从你的答案衍生出来的。非常感谢你提供的见解