-->

Basic GUI File Browser on Python

Here’s a helpful and interesting piece of code. It’s an extremely basic file browser that’s presented in a graphical user interface using the Tkinter module. There’s a lot you can learn from this code and implement into your own programs.

Basic GUI File Browser
Basic GUI File Browser 


Basic GUI File Browser



FILEBROWSER.PY

Tkinter is the main module in use here but we’re also using idlelib, so you may need to pip install any extras if the dependencies fail when you execute the code.

  1. from tkinter import Tk
  2. from idlelib.TreeWidget import ScrolledCanvas,
  3. FileTreeItem, TreeNode
  4.  
  5. import os
  6.  
  7. root = Tk()
  8. root.title(“File Browser”)
  9.  
  10. sc = ScrolledCanvas(root, bg=”white”,
  11. highlightthickness=0, takefocus=1)
  12. sc.frame.pack(expand=1, fill=”both”, side=”left”)
  13.  
  14. item = FileTreeItem(os.getcwd())
  15. node = TreeNode(sc.canvas, None, item)
  16. node.expand()
  17.  
  18. root.mainloop()

Basic GUI File Browser


Basic GUI File Browser


Advanced Filing

When executed, the code will display the current directory’s contents. If you want to see the contents of another directory, you can run the code from a command line within the chosen directory; just remember to call the code from where it’s located on your system, as per the second screenshot. You can also double-click any of the file names shown in the directory tree and rename them.

This is an interesting piece of code and one that you can insert into your own programs. You can extend the code to include a user specified directory to browse, perhaps your own unique file icons too. If you’re using Linux, create an alias to execute the code and then you can run it from wherever you are in the system.

Windows users may have some trouble with the above code, an alternative can be achieved by using the following:

  1. from tkinter import *
  2. from tkinter import ttk
  3. from tkinter.filedialog import askopenfilename
  4. root = Tk( )
  5. def OpenFile():
  6.     name = askopenfilename(initialdir=”C:/”,
  7.                 filetypes =((“Text File”, “*.txt”),(“All
  8.                 Files”,”*.*”)),
  9.                 title = “Choose a file.”
  10.                         )
  11.     print (name)
  12.         try:
  13.     with open(name,’r’) as UseFile:
  14.         print(UseFile.read())
  15.  
  16. except:
  17.     print(“No files opened”)
  18.     Title = root.title( “File Opener”)
  19.     label = ttk.Label(root, text =”File
  20.     Open”,foreground=”red”,font=(“Helvetica”, 16))
  21.     label.pack()
  22.  
  23. menu = Menu(root)
  24.  
  25. root.config(menu=menu)
  26. file = Menu(menu)
  27. file.add _ command(label = ‘Open’, command = OpenFile)
  28. file.add _ command(label = ‘Exit’, command =
  29. lambda:exit())
  30.  
  31. menu.add _ cascade(label = ‘File’, menu = file)
  32.  
  33. root.mainloop()


It’s not quite the same but this code allows you to open files in your system via the familiar Windows Explorer. It’s worth experimenting with to see what you can do with it.



0 Response to "Basic GUI File Browser on Python"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel