Skip to content

Commit ca2b21e

Browse files
committed
💄 Now displays all files and dirs in the home directory.
1 parent c5e2808 commit ca2b21e

File tree

3 files changed

+74
-4
lines changed

3 files changed

+74
-4
lines changed

dialogs.py

Lines changed: 74 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""All the dialogs, made from their base class."""
22

33
import glob
4+
import os
5+
import time
46
import tkinter
57

68
from tkinter import ttk
@@ -19,21 +21,43 @@ def __init__(self, master, *args, **kwargs):
1921
self.grab_set()
2022

2123
# Set the dialog's size
22-
self.wm_attributes("-zoomed", True)
24+
self.wm_geometry("900x700+20+20")
2325

26+
# The file and directory images
27+
FILE_IMAGE = os.path.join(
28+
os.path.dirname(os.path.abspath(__file__)),
29+
"res",
30+
"file.png"
31+
)
32+
self.file_image = tkinter.PhotoImage(file=FILE_IMAGE).subsample(4)
33+
DIR_IMAGE = os.path.join(
34+
os.path.dirname(os.path.abspath(__file__)),
35+
"res",
36+
"dir.png"
37+
)
38+
self.dir_image = tkinter.PhotoImage(file=DIR_IMAGE).subsample(4)
39+
2440
# Create all the dialog's widgets, and set its theme
2541
self.search_button = ttk.Button(self, text="Search...")
2642
self.search_button.grid(row=0, column=0, sticky="W")
2743
style = ttk.Style()
2844
style.theme_use("clam")
2945
self.__create_widgets()
46+
47+
# Open the home directory
48+
self.__show_dir(os.environ["HOME"])
3049

3150
def __create_widgets(self):
3251
"""Create all the widgets for the file dialog."""
3352

3453
# The treeview for the files
3554
self.treeview = ttk.Treeview(self, columns=("size", "modified"))
36-
self.treeview.grid(row=1, column=0, sticky="NSEW")
55+
self.treeview.grid(row=1, column=0, sticky="nsew")
56+
57+
# The scrollbar for the treeview
58+
self.scrollbar = ttk.Scrollbar(self, command=self.treeview.yview)
59+
self.scrollbar.grid(row=1, column=1, sticky="ns")
60+
self.treeview.config(yscrollcommand=self.scrollbar.set)
3761

3862
# The name column
3963
self.treeview.heading("#0", text="Name")
@@ -51,12 +75,58 @@ def __create_widgets(self):
5175
self.columnconfigure(0, weight=1)
5276
self.rowconfigure(1, weight=1)
5377

54-
def __open_dir(self, dir):
55-
"""Display the contents of directory DIR."""
78+
def __open_dir(self, directory):
79+
"""Return two sorted lists (directories, files) of the contents of directory."""
80+
81+
# The lists
82+
files = []
83+
dirs = []
84+
85+
# Get the contents of the directory
86+
contents = os.listdir(directory)
87+
88+
# Sort through the contents
89+
for c in contents:
90+
if os.path.isdir(os.path.join(directory, c)):
91+
dirs.append(os.path.join(directory, c))
92+
else:
93+
files.append(os.path.join(directory, c))
94+
dirs.sort()
95+
files.sort()
96+
97+
return dirs, files
98+
99+
def __show_dir(self, directory):
100+
"""Display the contents of directory DIRECTORY."""
56101

57102
# Delete all the old rows
58103
for i in self.treeview.get_children():
59104
self.treeview.delete(i)
105+
106+
# Get the contents of the directory
107+
dirs, files = self.__open_dir(directory)
108+
109+
# Display the directories
110+
for d in dirs:
111+
self.treeview.insert(
112+
"",
113+
"end",
114+
d + os.path.sep,
115+
text=os.path.basename(d),
116+
image=self.dir_image,
117+
values=(f"{len(os.listdir(d))} item(s)", f"{time.ctime(os.path.getmtime(d))}")
118+
)
119+
120+
# Display the files
121+
for f in files:
122+
self.treeview.insert(
123+
"",
124+
"end",
125+
f,
126+
text=os.path.basename(f),
127+
image=self.file_image,
128+
values=(f"{os.path.getsize(f)} bytes", f"{time.ctime(os.path.getmtime(f))}")
129+
)
60130

61131
if __name__ == "__main__":
62132
# Testing

res/dir.png

1.09 KB
Loading

res/file.png

1.2 KB
Loading

0 commit comments

Comments
 (0)