1
1
"""All the dialogs, made from their base class."""
2
2
3
3
import glob
4
+ import os
5
+ import time
4
6
import tkinter
5
7
6
8
from tkinter import ttk
@@ -19,21 +21,43 @@ def __init__(self, master, *args, **kwargs):
19
21
self .grab_set ()
20
22
21
23
# Set the dialog's size
22
- self .wm_attributes ( "-zoomed" , True )
24
+ self .wm_geometry ( "900x700+20+20" )
23
25
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
+
24
40
# Create all the dialog's widgets, and set its theme
25
41
self .search_button = ttk .Button (self , text = "Search..." )
26
42
self .search_button .grid (row = 0 , column = 0 , sticky = "W" )
27
43
style = ttk .Style ()
28
44
style .theme_use ("clam" )
29
45
self .__create_widgets ()
46
+
47
+ # Open the home directory
48
+ self .__show_dir (os .environ ["HOME" ])
30
49
31
50
def __create_widgets (self ):
32
51
"""Create all the widgets for the file dialog."""
33
52
34
53
# The treeview for the files
35
54
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 )
37
61
38
62
# The name column
39
63
self .treeview .heading ("#0" , text = "Name" )
@@ -51,12 +75,58 @@ def __create_widgets(self):
51
75
self .columnconfigure (0 , weight = 1 )
52
76
self .rowconfigure (1 , weight = 1 )
53
77
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."""
56
101
57
102
# Delete all the old rows
58
103
for i in self .treeview .get_children ():
59
104
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
+ )
60
130
61
131
if __name__ == "__main__" :
62
132
# Testing
0 commit comments