Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit ad2bd2a

Browse filesBrowse files
committed
add file explorer tutorial
1 parent cf7be35 commit ad2bd2a
Copy full SHA for ad2bd2a

File tree

Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+114
-0
lines changed
Open diff view settings
Collapse file

‎README.md‎

Copy file name to clipboardExpand all lines: README.md
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -213,5 +213,6 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
213213
- [How to Make a Text Editor using Tkinter in Python](https://www.thepythoncode.com/article/text-editor-using-tkinter-python). ([code](gui-programming/text-editor))
214214
- [How to Make a Button using PyGame in Python](https://www.thepythoncode.com/article/make-a-button-using-pygame-in-python). ([code](gui-programming/button-in-pygame))
215215
- [How to Make a Drawing Program in Python](https://www.thepythoncode.com/article/make-a-drawing-program-with-python). ([code](gui-programming/drawing-tool-in-pygame))
216+
- [How to Make a File Explorer using Tkinter in Python](https://www.thepythoncode.com/article/create-a-simple-file-explorer-using-tkinter-in-python). ([code](gui-programming/file-explorer))
216217

217218
For any feedback, please consider pulling requests.
Collapse file
+1Lines changed: 1 addition & 0 deletions
  • Display the source diff
  • Display the rich diff
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Make a File Explorer using Tkinter in Python](https://www.thepythoncode.com/article/create-a-simple-file-explorer-using-tkinter-in-python)
Collapse file
+112Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.