【问题标题】:Python Tkinter askopenfile with entry boxPython Tkinter askopenfile 带输入框
【发布时间】:2019-12-10 21:34:32
【问题描述】:

我希望任何人都知道如何将 askopenfile 函数与输入框 (tkinter) 结合使用,以便显示文件路径,并且可以在使用 askopenfile 选择后在输入框中进行编辑。欢迎任何关于如何做到这一点的想法,谢谢!

【问题讨论】:

    标签: python python-3.x tkinter python-os


    【解决方案1】:

    这可以通过为这个小部件创建一个小的自定义类来完成

    我为此制作了一个小部件:

    from tkinter import *
    from tkinter.filedialog import *
    
    
    class FilePathFrame(Frame):
    
        def __init__(self, master, *args, **kwargs):
            super(FilePathFrame, self).__init__(master, *args, **kwargs)
    
            def entry_set(entry, text):
                entry.delete(0, 'end')
                entry.insert(END, text)
    
            item_label = Label(self, text="File Path: ", relief="flat", fg="gray40", anchor=W)
            item_label.pack()
            item_file = StringVar()
            item_entry = Entry(self, textvariable=item_file)
            item_entry.pack()
            item_button = Button(self, text="\uD83D\uDCC2", relief="groove",
                                 command=lambda: (
                                 entry_set(item_entry, askopenfilename()), item_entry.configure(fg="black")))
            item_button.pack()
    
    
    window = Tk()
    f = FilePathFrame(window)
    f.pack()
    window.mainloop()
    

    显然你需要弄清楚你想如何显示它,我只是使用.pack() 方法来显示所有内容。

    【讨论】:

    • 感谢您帮助我,孤独的英雄:)!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-06-09
    • 1970-01-01
    • 2018-09-17
    • 2020-05-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多