【问题标题】:Import multiple files with TK inter module使用 TK 模块间导入多个文件
【发布时间】:2014-05-11 18:46:11
【问题描述】:

我开发了一种将 vcf 文件导出到 excel 的算法。 现在我想制作一个用户界面,可以选择通过浏览按钮将多个 vcf 文件导入 1 个条目。

class MyFrame(Frame):
    def __init__(self):
        Frame.__init__(self)
        self.master.title("Vcf to excel converter")
        self.geometry("300x200")
        self.master.rowconfigure(5, weight=1)
        self.master.columnconfigure(5, weight=1)
        self.grid(sticky=W+E+N+S)

        self.entry=Entry(self)
        self.entry.grid(row=2, column=1)

        self.button = Button(self, text="Browse", command=self.load_file, width=10)
        self.button.grid(row=2, column=6, sticky=E)

        self.button1= Button(self, text="Convert", width=10)
        self.button1.grid(row=100, column=3, sticky=E)
    def load_file(self):
        fname = askopenfilename(filetypes=(("Vcards", "*.vcf"),
                                            ))
        if fname:
            try:
                print("""here it comes: self.settings["template"].set(fname)""")
            except:                     # <- naked except is a bad idea
                showerror("Open Source File", "Failed to read file\n'%s'" % fname)
            return

MyFrame().mainloop()

您在上面看到了 1 个 vcf 文件的算法。 这个想法是在这个浏览器中选择同一目录中的多个文件到一个条目中,然后将它导出到一个列表中,如:

[["D:\Documents\1.vcf"],[["D:\Documents\2.vcf"],["D:\Documents\3.vcf"]...]

所以我希望此列表中的完整目录用于我的功能。 有人现在可以用 Tkinter 做到这一点吗?

亲切的问候,

格伦

【问题讨论】:

    标签: python python-2.7 tkinter


    【解决方案1】:

    如果您想选择多个文件,请将s 添加到askopenfilename

    fnames = askopenfilenames()
    
    print(fnames.split())
    

    【讨论】:

    • 谢谢,但现在我无法将它们列入列表,因为我经常在我的方向前得到一个“u”,而我现在不知道它来自哪里,例如:[u'D: /Documents/1', u' D:/Documents/2', u' D:/Documents/3', u' D:/Documents/4', u'']
    • 这意味着它是一个unicode字符串,它对将返回值变成一个列表没有任何影响。 docs.python.org/2/tutorial/introduction.html#unicode-strings
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-03
    相关资源
    最近更新 更多