Below is a
Browse class that lets the user browse files and show the path in an entry frame. This is pretty short and basic.
import tkinter as tk from tkinter import filedialog as fd class Browse(tk.Frame): """ Creates a frame that contains a button when clicked lets the user to select a file and put its filepath into an entry. """ def __init__(self, master, initialdir='', filetypes=()): super().__init__(master) self.filepath = tk.StringVar() self._initaldir = initialdir self._filetypes = filetypes self._create_widgets() self._display_widgets() def _create_widgets(self): self._entry = tk.Entry(self, textvariable=self.filepath) self._button = tk.Button(self, text="Browse...", command=self.browse) def _display_widgets(self): self._entry.pack(fill='x', expand=True) self._button.pack(anchor='se') def browse(self): """ Browses a .png file or all files and then puts it on the entry. """ self.filepath.set(fd.askopenfilename(initialdir=self._initaldir, filetypes=self._filetypes)) if __name__ == '__main__': root = tk.Tk() file_browser = Browse(root, initialdir=r"C:\Users", filetypes=(('Portable Network Graphics','*.png'), ("All files", "*.*"))) file_browser.pack(fill='x', expand=True) root.mainloop()
- My main concern is to code in an easy to read, efficient, and well-structured manner while still learning the language and concepts such as OOP. Feel free to mention the tiniest issue or improvement that comes to your mind, as I am a beginner and I probably need it.