|
| 1 | +from tkinter import * |
| 2 | +import os |
| 3 | +import ctypes |
| 4 | +import pathlib |
| 5 | + |
| 6 | +# Increase Dots Per inch so it looks sharper |
| 7 | +ctypes.windll.shcore.SetProcessDpiAwareness(True) |
| 8 | + |
| 9 | +root = Tk() |
| 10 | +# set a title for our file explorer main window |
| 11 | +root.title('Simple Explorer') |
| 12 | + |
| 13 | +root.grid_columnconfigure(1, weight=1) |
| 14 | +root.grid_rowconfigure(1, weight=1) |
| 15 | + |
| 16 | +def pathChange(*event): |
| 17 | + # Get all Files and Folders from the given Directory |
| 18 | + directory = os.listdir(currentPath.get()) |
| 19 | + # Clearing the list |
| 20 | + list.delete(0, END) |
| 21 | + # Inserting the files and directories into the list |
| 22 | + for file in directory: |
| 23 | + list.insert(0, file) |
| 24 | + |
| 25 | +def changePathByClick(event=None): |
| 26 | + # Get clicked item. |
| 27 | + picked = list.get(list.curselection()[0]) |
| 28 | + # get the complete path by joining the current path with the picked item |
| 29 | + path = os.path.join(currentPath.get(), picked) |
| 30 | + # Check if item is file, then open it |
| 31 | + if os.path.isfile(path): |
| 32 | + print('Opening: '+path) |
| 33 | + os.startfile(path) |
| 34 | + # Set new path, will trigger pathChange function. |
| 35 | + else: |
| 36 | + currentPath.set(path) |
| 37 | + |
| 38 | +def goBack(event=None): |
| 39 | + # get the new path |
| 40 | + newPath = pathlib.Path(currentPath.get()).parent |
| 41 | + # set it to currentPath |
| 42 | + currentPath.set(newPath) |
| 43 | + # simple message |
| 44 | + print('Going Back') |
| 45 | + |
| 46 | + |
| 47 | +def open_popup(): |
| 48 | + global top |
| 49 | + top = Toplevel(root) |
| 50 | + top.geometry("250x150") |
| 51 | + top.resizable(False, False) |
| 52 | + top.title("Child Window") |
| 53 | + top.columnconfigure(0, weight=1) |
| 54 | + Label(top, text='Enter File or Folder name').grid() |
| 55 | + Entry(top, textvariable=newFileName).grid(column=0, pady=10, sticky='NSEW') |
| 56 | + Button(top, text="Create", command=newFileOrFolder).grid(pady=10, sticky='NSEW') |
| 57 | + |
| 58 | +def newFileOrFolder(): |
| 59 | + # check if it is a file name or a folder |
| 60 | + if len(newFileName.get().split('.')) != 1: |
| 61 | + open(os.path.join(currentPath.get(), newFileName.get()), 'w').close() |
| 62 | + else: |
| 63 | + os.mkdir(os.path.join(currentPath.get(), newFileName.get())) |
| 64 | + # destroy the top |
| 65 | + top.destroy() |
| 66 | + pathChange() |
| 67 | + |
| 68 | +top = '' |
| 69 | + |
| 70 | +# String variables |
| 71 | +newFileName = StringVar(root, "File.dot", 'new_name') |
| 72 | +currentPath = StringVar( |
| 73 | + root, |
| 74 | + name='currentPath', |
| 75 | + value=pathlib.Path.cwd() |
| 76 | +) |
| 77 | +# Bind changes in this variable to the pathChange function |
| 78 | +currentPath.trace('w', pathChange) |
| 79 | + |
| 80 | +Button(root, text='Folder Up', command=goBack).grid( |
| 81 | + sticky='NSEW', column=0, row=0 |
| 82 | +) |
| 83 | + |
| 84 | +# Keyboard shortcut for going up |
| 85 | +root.bind("<Alt-Up>", goBack) |
| 86 | + |
| 87 | +Entry(root, textvariable=currentPath).grid( |
| 88 | + sticky='NSEW', column=1, row=0, ipady=10, ipadx=10 |
| 89 | +) |
| 90 | + |
| 91 | +# List of files and folder |
| 92 | +list = Listbox(root) |
| 93 | +list.grid(sticky='NSEW', column=1, row=1, ipady=10, ipadx=10) |
| 94 | + |
| 95 | +# List Accelerators |
| 96 | +list.bind('<Double-1>', changePathByClick) |
| 97 | +list.bind('<Return>', changePathByClick) |
| 98 | + |
| 99 | + |
| 100 | +# Menu |
| 101 | +menubar = Menu(root) |
| 102 | +# Adding a new File button |
| 103 | +menubar.add_command(label="Add File or Folder", command=open_popup) |
| 104 | +# Adding a quit button to the Menubar |
| 105 | +menubar.add_command(label="Quit", command=root.quit) |
| 106 | +# Make the menubar the Main Menu |
| 107 | +root.config(menu=menubar) |
| 108 | + |
| 109 | +# Call the function so the list displays |
| 110 | +pathChange('') |
| 111 | +# run the main program |
| 112 | +root.mainloop() |
0 commit comments