-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
234 lines (180 loc) · 7.62 KB
/
main.py
File metadata and controls
234 lines (180 loc) · 7.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
# -------------------------------- IMPORT MODULES --------------------------------
from tkinter import *
from tkinter import filedialog as tfd
from tkinter import messagebox as tmb
from PIL import ImageTk, Image, ImageDraw, ImageFont
import os
# -------------------------------- COLORS --------------------------------
bgColor = "#237b90"
btnBgColor = "#6c0102"
hintColor = "#b3b3b3"
# -------------------------------- TKINTER SETUP --------------------------------
# Creating an instance of tkinter
root = Tk()
# Adding title
root.title("Directory Tree Generator")
# Setting icon
#root.iconbitmap("icon.ico")
# Setting default size
root.geometry("800x500")
# Setting minimum size
root.minsize(800, 500)
# Setting background
root.configure(bg=bgColor)
# -------------------------------- STRINGS --------------------------------
dir_hint = "Enter Directory"
file_hint = "Enter file name"
empty_dir_msg = "No directory has given."
empty_file_msg = "No file has given.\nPlease Enter file name to search in the given directory."
about_msg = "Directory Tree Generator to map the directory.\n\nChoose directory either by clicking on 'BROWSE DIRECTORY' button or entering the directory path.\nClick on 'RUN' button, tree will be generated for the choosen directory.\n\nYou can search a file in the choosed directoy by entering the file name then click the 'SEARCH FILE' button.\n\n\n\nCreated by farizma (GitHub @fari-zma)"
# -------------------------------- VARIABLES --------------------------------
dirPath = StringVar()
dirPath.set(dir_hint)
fileName = StringVar()
fileName.set(file_hint)
# -------------------------------- FUNCTIONS --------------------------------
def clearDirHint(event):
if dirPath.get() == dir_hint:
dir_entry.config(fg="black")
dirPath.set("")
def clearFileHint(event):
if fileName.get() == file_hint:
file_entry.config(fg="black")
fileName.set("")
def browseEnter(event):
run()
def searchFileOnEnter(event):
searchFile()
def browse():
dir_selected = tfd.askdirectory()
dir_entry.config(fg="black")
dirPath.set(dir_selected.replace('/', '\\'))
def isEmpty():
if dirPath.get() == "" or dirPath.get() == dir_hint:
textArea.config(state="normal")
textArea.delete("1.0", END)
textArea.config(state="disabled")
tmb.showerror("Directory Tree Generator", empty_dir_msg)
return True
else:
return False
def run():
if isEmpty()== False:
textArea.config(state="normal")
path = dirPath.get()
textArea.delete("1.0", END)
textArea.insert(END, path)
for root, dirs, files in os.walk(path):
root = root.replace(path, "")
# count the seperator -> it tells the level
level = root.count(os.sep)
if level == 0:
textArea.insert(END, root + "\n")
for file in files:
textArea.insert(END, " "*level + "|--" + file + "\n")
else:
textArea.insert(END, "|" + "--"*level)
textArea.insert(END, root.replace('\\', '') + "\n")
for file in files:
textArea.insert(END, "|" + " "*level + "|--" + file + "\n")
textArea.insert(END, "|\n")
textArea.config(state="disabled")
def searchFile():
if isEmpty()== False:
file_name = fileName.get()
if file_name == "" or file_name == file_hint:
tmb.showerror("Directory Tree Generator", empty_file_msg)
else:
textArea.config(state="normal")
textArea.delete("1.0", END)
for root, dirs, files in os.walk(dirPath.get()):
for file in files:
if file_name in file:
textArea.insert(END, os.path.join(root,file) + "\n")
textArea.config(state="disabled")
def getWidth():
max_ch = 0
no_of_char = 0
for ch in textArea.get("1.0", END):
if ch == "\n":
if no_of_char > max_ch:
max_ch = no_of_char
no_of_char = 0
else:
no_of_char += 1
return max_ch*9
def getHeight():
lines = 0
for ch in textArea.get("1.0", END):
if ch == "\n":
lines += 1
return lines*19
def save():
if (textArea.get("1.0", END)) == "\n":
tmb.showerror("Cannot save", "Nothing to save.")
return
font = ImageFont.truetype('bahnschrift.ttf', 18)
image = Image.new('RGB', (getWidth(), getHeight()), color = 'white')
draw = ImageDraw.Draw(image)
draw.text((0,0), textArea.get("1.0", END), fill="black", font=font)
filename = tfd.asksaveasfilename(initialfile="Untitled.jpg" ,defaultextension=".jpg",
filetypes=[('JPG file','.jpg'), ('JPEG file','.jpeg'), ('all files','*.*')])
if filename is None:
return
else:
image.save(filename)
tmb.showinfo('SAVED', 'File has been saved successfully!')
def clear():
textArea.config(state="normal")
textArea.delete("1.0", END)
textArea.config(state="disabled")
dir_entry.config(fg=hintColor)
file_entry.config(fg=hintColor)
dirPath.set(dir_hint)
fileName.set(file_hint)
def about():
tmb.showinfo("About", about_msg)
def exit():
result = tmb.askquestion("Exit", "Are you sure you want to exit?", icon="warning")
if result == "yes":
root.destroy()
# -------------------------------- FRAMES --------------------------------
browse_btn = Button(root, text="BROWSE DIRECTORY", bg="#a6b401", fg="white", command=browse, padx=5)
browse_btn.pack(padx=5, pady=5)
browse_frame = Frame(root, width=600, height=50, bg=bgColor)
browse_frame.pack(padx=5, pady=5)
run_btn = Button(browse_frame, width=10, text="RUN", command=run, bg=btnBgColor, fg="white")
run_btn.pack(side=RIGHT, padx=5, pady=5)
dir_entry = Entry(browse_frame, width=200, bd=2, font="consolas 12", textvariable=dirPath, fg=hintColor)
dir_entry.pack(side=RIGHT, padx=5, pady=5)
dir_entry.bind('<Button-1>', clearDirHint)
dir_entry.bind('<Return>', browseEnter)
file_frame = Frame(root, width=600, height=50, bg=bgColor)
file_frame.pack(padx=5, pady=5)
file_btn = Button(file_frame, width=10, text="SEARCH FILE", command=searchFile, bg=btnBgColor, fg="white")
file_btn.pack(side=RIGHT, padx=5, pady=5)
file_entry = Entry(file_frame, width=200, bd=2, font="consolas 12", textvariable=fileName, fg="#b3b3b3")
file_entry.pack(side=RIGHT, padx=5, pady=5)
file_entry.bind('<Button-1>', clearFileHint)
file_entry.bind('<Return>', searchFileOnEnter)
buttons_frame = Frame(root, width=600, height=50, bg=bgColor)
buttons_frame.pack(padx=5, pady=5)
save_btn = Button(buttons_frame, width=10, text="SAVE", command=save, bg=btnBgColor, fg="white")
save_btn.pack(side=LEFT, padx=5, pady=5)
clear_btn = Button(buttons_frame, width=10, text="CLEAR", command=clear, bg=btnBgColor, fg="white")
clear_btn.pack(side=LEFT, padx=5, pady=5)
about_btn = Button(buttons_frame, width=10, text="ABOUT", command=about, bg=btnBgColor, fg="white")
about_btn.pack(side=LEFT, padx=5, pady=5)
exit_btn = Button(buttons_frame, width=10, text="EXIT", command=exit, bg=btnBgColor, fg="white")
exit_btn.pack(side=LEFT, padx=5, pady=5)
# -------------------------------- RESULT_WINDOW + SCROLLBARS --------------------------------
sby = Scrollbar(root, orient=VERTICAL)
sby.pack(side=RIGHT, fill=Y)
sbx = Scrollbar(root, orient=HORIZONTAL)
sbx.pack(side=BOTTOM, fill=X)
textArea = Text(root, wrap=NONE, state="disabled" ,yscrollcommand=sby.set, xscrollcommand=sbx.set, font="consolas 11")
textArea.pack(side=LEFT, fill=BOTH, expand=1)
sby.config(command=textArea.yview)
sbx.config(command=textArea.xview)
if __name__ == "__main__":
root.mainloop()