【问题标题】:How to get filepaths properly from tkinterDND2 event object如何从 tkinterDND2 事件对象中正确获取文件路径
【发布时间】:2018-08-15 05:53:00
【问题描述】:

我正在尝试在 python 中使用 tkinter 在一个简单的 GUI 应用程序中实现“将文件放在此处”功能

它使用了 TkinterDnD2,最终看到这个stackoverflow answer

删除文件后 event.data 返回用大括号括起来的文件名

示例:{d:/test/sample1.pdf}

但是,当多个文件被放到目标中时,每个路径都被花括号括起来

例如:{D:/test/sample1.pdf} {D:/test/sample2.pdf}

如何正确解析此字符串中的文件路径?

【问题讨论】:

  • 贴出你写的代码。否则首先对其进行编码并使用您的代码编辑帖子
  • 示例是 event.data 返回的字符串。也就是说,每个路径都被花括号包围。我只想从该字符串中提取路径。

标签: python user-interface tkinter


【解决方案1】:

使用这两行python代码可以轻松实现:

temp = filePathsReturned[1:-1]
list = temp.split("} {")

【讨论】:

  • 是的,效果很好,继续。如果路径之间没有间隙并且我的答案中突出显示了多个 {} 大括号,我的答案效果很好
  • 其实这个答案只是你的答案的衍生......非常感谢你提供的见解
【解决方案2】:

我不确定 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)

对于上面的字符串输出是

['mypath}}}}1/tofil{{{e1', 'mypath3/tofile3', 'mypath2/tofile2}}}}}']

如果str1是

str1="{{{mypath1/tofil}}e1}{mypath3/tofile3}{mypath2/tofile2}}}}}}"

输出是

['{{mypath1/tofil}}e1', 'mypath3/tofile3', 'mypath2/tofile2}}}}}']

也适用于其他测试用例。这段代码运行良好。

【讨论】:

  • 我想过做这样的事情..但是如果文件名中有花括号,这可能会成为一个问题..
  • @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 { 之间有间隔。这不是预期的。这又是很多工作。以后可能会对您有所帮助。
  • 非常感谢您的努力。我自己解决了
猜你喜欢
  • 1970-01-01
  • 2013-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多