<small id='sIEa7'></small><noframes id='sIEa7'>

    1. <tfoot id='sIEa7'></tfoot>

      <i id='sIEa7'><tr id='sIEa7'><dt id='sIEa7'><q id='sIEa7'><span id='sIEa7'><b id='sIEa7'><form id='sIEa7'><ins id='sIEa7'></ins><ul id='sIEa7'></ul><sub id='sIEa7'></sub></form><legend id='sIEa7'></legend><bdo id='sIEa7'><pre id='sIEa7'><center id='sIEa7'></center></pre></bdo></b><th id='sIEa7'></th></span></q></dt></tr></i><div id='sIEa7'><tfoot id='sIEa7'></tfoot><dl id='sIEa7'><fieldset id='sIEa7'></fieldset></dl></div>
        • <bdo id='sIEa7'></bdo><ul id='sIEa7'></ul>
        <legend id='sIEa7'><style id='sIEa7'><dir id='sIEa7'><q id='sIEa7'></q></dir></style></legend>

        如何将对话框窗口中的选定文件添加到字典中?

        时间:2023-06-05

            • <bdo id='wbNSr'></bdo><ul id='wbNSr'></ul>

              <i id='wbNSr'><tr id='wbNSr'><dt id='wbNSr'><q id='wbNSr'><span id='wbNSr'><b id='wbNSr'><form id='wbNSr'><ins id='wbNSr'></ins><ul id='wbNSr'></ul><sub id='wbNSr'></sub></form><legend id='wbNSr'></legend><bdo id='wbNSr'><pre id='wbNSr'><center id='wbNSr'></center></pre></bdo></b><th id='wbNSr'></th></span></q></dt></tr></i><div id='wbNSr'><tfoot id='wbNSr'></tfoot><dl id='wbNSr'><fieldset id='wbNSr'></fieldset></dl></div>
              <legend id='wbNSr'><style id='wbNSr'><dir id='wbNSr'><q id='wbNSr'></q></dir></style></legend>

              <small id='wbNSr'></small><noframes id='wbNSr'>

                    <tbody id='wbNSr'></tbody>
                  <tfoot id='wbNSr'></tfoot>
                • 本文介绍了如何将对话框窗口中的选定文件添加到字典中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我希望它能够打开一个对话窗口并选择我的文件,

                  I wish it's able to open a dialog window and select my files,

                  a.txt
                  b.txt
                  

                  然后将它们添加到我的字典中

                  then add them in my dictionary

                  myDict = { "a.txt" : 0,
                             "b.txt" : 1}
                  

                  我在网站上搜索过

                  import Tkinter,tkFileDialog
                  root = Tkinter.Tk()
                  filez = tkFileDialog.askopenfilenames(parent=root,multiple='multiple',title='Choose a file')
                  

                  这些代码用于打开对话窗口并选择我的文件.但问题是如何将选中的文件添加到字典中?

                  these codes work for opening a dialog window and selecting my files. But the question is how to add the selected files to the dictionary?

                  有了斯蒂芬的回答,问题就解决了

                  With Stephan's answer, the problem is solved

                  myDict = {}
                  for filename in filez:
                      myDict[filename] = len(myDict)
                      print "myDict: " + str(myDict)
                  

                  现在 myDict 是

                  Now the myDict is

                  myDict = {'C:/a.txt': 0}
                  myDict = {'C:/a.txt': 0, 'C:/b.txt': 1}
                  

                  网上搜索后,添加os.path.split

                  After searching online, just add os.path.split

                  myDict = {}
                  for filename in filez:
                      head, tail = os.path.split(str(filename))
                      myDict[tail] = len(myDict)
                  

                  现在一切正常

                  myDict = {'a.txt': 0, 'b.txt': 1}
                  

                  我得到了没有路径的 myDict,问题解决了!谢谢!

                  I got the myDict without path, problem solved! Thanks!

                  推荐答案

                  myDict = {}
                  myDict[filenameFromDialog] = len(myDict)
                  

                  这是添加到字典的语法.

                  That is the syntax for adding to a dictionary.

                  如果您有一组文件要添加到字典中,您可以遍历列表并一次添加一个:

                  If you have an array of files you want to add to the dictionary, you could loop over the list and add them one at a time:

                  myDict = {}
                  for filename in filez:
                      myDict[filename] = len(myDict)
                  

                  这篇关于如何将对话框窗口中的选定文件添加到字典中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何显示 PyQt 模式对话框并在关闭后从其控件中获取数据? 下一篇:在 PyQt 中打开第二个窗口

                  相关文章

                  <tfoot id='8pvAq'></tfoot>

                    <small id='8pvAq'></small><noframes id='8pvAq'>

                    <legend id='8pvAq'><style id='8pvAq'><dir id='8pvAq'><q id='8pvAq'></q></dir></style></legend>
                      <bdo id='8pvAq'></bdo><ul id='8pvAq'></ul>

                      <i id='8pvAq'><tr id='8pvAq'><dt id='8pvAq'><q id='8pvAq'><span id='8pvAq'><b id='8pvAq'><form id='8pvAq'><ins id='8pvAq'></ins><ul id='8pvAq'></ul><sub id='8pvAq'></sub></form><legend id='8pvAq'></legend><bdo id='8pvAq'><pre id='8pvAq'><center id='8pvAq'></center></pre></bdo></b><th id='8pvAq'></th></span></q></dt></tr></i><div id='8pvAq'><tfoot id='8pvAq'></tfoot><dl id='8pvAq'><fieldset id='8pvAq'></fieldset></dl></div>