Advertisement
Techpad

Files 8

Mar 20th, 2021
272
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 11.10 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import simpledialog as spd
  3. from tkinter import messagebox as mb
  4. import os
  5. try:
  6. import shutil
  7. except ModuleNotFoundError:
  8. os.system('pip install shutil')
  9. import shutil
  10.  
  11. _root = frame1
  12.  
  13. topbar = tk.Frame(_root, bd=1, relief='solid', bg='white', height=30)
  14. dirframe = tk.Frame(_root, bd=0, bg='white')
  15.  
  16. topbar.pack(fill='x')
  17. dirframe.pack(expand=True, fill='both')
  18.  
  19. global CURRENT_PATH
  20. CURRENT_PATH = 'T:\\Storage'
  21.  
  22. SYSTEM_DIRECTORIES = [
  23. r"T:\BIOS Programs",
  24. r"T:\TechOS",
  25. r"T:\ProgramData",
  26. r"T:\ProgramData\Info",
  27. r"T:\TechOS\Virtual",
  28. r"T:\TechOS\Virtual\Images",
  29. r"T:\TechOS\Virtual\Images\Wallpapers",
  30. r"T:\TechOS\Virtual\Info",
  31. r"T:\TechOS\Virtual\Settings",
  32. r"T:\TechOS\Virtual\Cursors"
  33. ]
  34.  
  35. def goback():
  36. prvpath = os.path.dirname(CURRENT_PATH)
  37. global directory
  38. directory(prvpath)
  39.  
  40. back = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', font='TkDefaultFont 20', width=30, height=30, command=goback)
  41. back.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Back.png')
  42. back.config(image=back.image)
  43. back.pack(side='left', anchor='w', fill='y')
  44.  
  45. dirlabel = tk.Label(topbar, bg='white', text=CURRENT_PATH)
  46. dirlabel.pack(side='left', anchor='w', fill='y')
  47.  
  48. def rename(path):
  49. global SYSTEM_DIRECTORIES
  50. global os
  51. global mb
  52. global spd
  53. global CURRENT_PATH
  54. if any(i in path for i in SYSTEM_DIRECTORIES):
  55. mb.showinfo("Permission Denied", "You do not have permission to rename this file.")
  56. else:
  57. newfilename = spd.askstring("Rename file", "Rename \"" + os.path.basename(path) + "\":")
  58. if newfilename == None:
  59. pass
  60. else:
  61. path0 = os.path.dirname(path)
  62. try:
  63. os.rename(path, os.path.join(path0, newfilename))
  64. except:
  65. mb.showinfo("Error", "Could not rename file")
  66. directory(CURRENT_PATH)
  67.  
  68. def delete(path):
  69. global SYSTEM_DIRECTORIES
  70. global os
  71. global mb
  72. global CURRENT_PATH
  73. global directory
  74. global shutil
  75. if any(i in path for i in SYSTEM_DIRECTORIES):
  76. mb.showinfo("Permission Denied", "You do not have permission to delete this file.")
  77. else:
  78. if os.path.isfile(path):
  79. try:
  80. os.remove(path)
  81. except:
  82. mb.showinfo("Error", "Could not delete file")
  83. elif os.path.isdir(path):
  84. continuedelete = mb.askyesno("Delete Folder", "Are you sure you want to delete this folder and all its files?")
  85. if continuedelete == True:
  86. try:
  87. shutil.rmtree(path)
  88. except:
  89. mb.showinfo("Error", "Could not delete folder")
  90. else:
  91. pass
  92. directory(CURRENT_PATH)
  93.  
  94. def newfile():
  95. global CURRENT_PATH
  96. global directory
  97. filepath1 = os.path.join(CURRENT_PATH, 'Untitled')
  98. rep = 0
  99. while True:
  100. if os.path.exists(filepath1 + str(rep) + '.txt'):
  101. rep += 1
  102. else:
  103. filepath0 = filepath1 + str(rep) + '.txt'
  104. break
  105. NEW_FILE = open(filepath0, 'x')
  106. NEW_FILE.close()
  107. directory(CURRENT_PATH)
  108.  
  109. def newfolder():
  110. global CURRENT_PATH
  111. global directory
  112. folderpath1 = os.path.join(CURRENT_PATH, 'New Folder')
  113. if os.path.exists(folderpath1):
  114. rep = 1
  115. while True:
  116. if os.path.exists(folderpath1 + ' (' + str(rep) + ')'):
  117. rep += 1
  118. else:
  119. folderpath0 = folderpath1 + ' (' + str(rep) + ')'
  120. break
  121. os.mkdir(folderpath0)
  122. else:
  123. os.mkdir(folderpath1)
  124. directory(CURRENT_PATH)
  125.  
  126. newfilebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ File ', command=newfile)
  127. newfilebutton.pack(side='right', anchor='e', fill='y')
  128.  
  129. newfolderbutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ Folder', command=newfolder)
  130. newfolderbutton.pack(side='right', anchor='e', fill='y')
  131.  
  132. def directory(path):
  133. global spd
  134. global mb
  135. global dirframe
  136. global dirlabel
  137. global CURRENT_PATH
  138. global _root
  139. global SYSTEM_DIRECTORIES
  140. global rename
  141. global delete
  142. global tk
  143. if any(path == i for i in SYSTEM_DIRECTORIES):
  144. mb.showinfo("Careful!", "The directory you are about to enter contains system files. Modifying these files might cause irreparable damage to your system.")
  145. CURRENT_PATH = path
  146. dirlabel.config(text=path)
  147. for child in dirframe.winfo_children():
  148. child.destroy()
  149. try:
  150. fullcontents = os.listdir(path)
  151. contents = [file for file in fullcontents if not file == 'desktop.ini' and not file == 'System Volume Information' and not file == '$RECYCLE.BIN' and not file.endswith('.{SYSTEM}')]
  152. for element in contents:
  153. fullpath = os.path.join(path, element)
  154. if os.path.isdir(fullpath):
  155. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: directory(fullpath), padx=5, pady=2)
  156. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Folder.png')
  157. tempbutton.config(image=tempbutton.image)
  158. tempbutton.pack(fill='x', anchor='w', side='top')
  159. tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  160. tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  161. elif os.path.isfile(fullpath):
  162. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: openfile(fullpath), padx=5, pady=2)
  163. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/File.png')
  164. tempbutton.config(image=tempbutton.image)
  165. tempbutton.pack(fill='x', anchor='w', side='top')
  166. tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  167. tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  168. except NotADirectoryError:
  169. spd.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  170.  
  171. def directory_rename():
  172. global spd
  173. global mb
  174. global dirframe
  175. global dirlabel
  176. global CURRENT_PATH
  177. global _root
  178. global SYSTEM_DIRECTORIES
  179. global rename
  180. global delete
  181. global tk
  182. global directory
  183. path = CURRENT_PATH
  184. dirlabel.config(text=path)
  185. for child in dirframe.winfo_children():
  186. child.destroy()
  187. try:
  188. fullcontents = os.listdir(path)
  189. contents = [file for file in fullcontents if not file == 'desktop.ini' and not file == 'System Volume Information' and not file == '$RECYCLE.BIN' and not file.endswith('.{SYSTEM}')]
  190. for element in contents:
  191. fullpath = os.path.join(path, element)
  192. if os.path.isdir(fullpath):
  193. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: rename(fullpath), padx=5, pady=2)
  194. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderRename.png')
  195. tempbutton.config(image=tempbutton.image)
  196. tempbutton.pack(fill='x', anchor='w', side='top')
  197. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  198. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  199. elif os.path.isfile(fullpath):
  200. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: rename(fullpath), padx=5, pady=2)
  201. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileRename.png')
  202. tempbutton.config(image=tempbutton.image)
  203. tempbutton.pack(fill='x', anchor='w', side='top')
  204. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  205. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  206. except NotADirectoryError:
  207. spd.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  208.  
  209. def directory_delete():
  210. global spd
  211. global mb
  212. global dirframe
  213. global dirlabel
  214. global CURRENT_PATH
  215. global _root
  216. global SYSTEM_DIRECTORIES
  217. global rename
  218. global delete
  219. global tk
  220. global directory
  221. path = CURRENT_PATH
  222. dirlabel.config(text=path)
  223. for child in dirframe.winfo_children():
  224. child.destroy()
  225. try:
  226. fullcontents = os.listdir(path)
  227. contents = [file for file in fullcontents if not file == 'desktop.ini' and not file == 'System Volume Information' and not file == '$RECYCLE.BIN' and not file.endswith('.{SYSTEM}')]
  228. for element in contents:
  229. fullpath = os.path.join(path, element)
  230. if os.path.isdir(fullpath):
  231. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: delete(fullpath), padx=5, pady=2)
  232. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderDelete.png')
  233. tempbutton.config(image=tempbutton.image)
  234. tempbutton.pack(fill='x', anchor='w', side='top')
  235. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  236. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  237. elif os.path.isfile(fullpath):
  238. tempbutton = tk.Button(dirframe, text=' ' + element, anchor='w', bd=0, bg='white', activebackground='lightgray', compound='left', command=lambda fullpath=fullpath: delete(fullpath), padx=5, pady=2)
  239. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileDelete.png')
  240. tempbutton.config(image=tempbutton.image)
  241. tempbutton.pack(fill='x', anchor='w', side='top')
  242. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  243. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  244. except NotADirectoryError:
  245. spd.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  246.  
  247. deletebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Delete', command=directory_delete)
  248. deletebutton.pack(side='right', anchor='e', fill='y')
  249.  
  250. renamebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Rename', command=directory_rename)
  251. renamebutton.pack(side='right', anchor='e', fill='y')
  252.  
  253. dirframe.bind("<Button>", lambda event, cp=CURRENT_PATH: directory(cp))
  254.  
  255. directory(CURRENT_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement