Python Tkinter askopenfile with entry box



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

这将通过为此小部件创建一个小型自定义类来完成

我有一个出于这个特定原因而制作的小部件:

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="uD83DuDCC2", 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()方法来显示所有内容。

最新更新