Advertisement
Techpad

Files 12

May 4th, 2021
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 14.85 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. from threading import Thread
  6. import time
  7. try:
  8. import shutil
  9. except ModuleNotFoundError:
  10. os.system('pip install shutil')
  11. import shutil
  12.  
  13. _root = frame1
  14.  
  15. topbar = tk.Frame(_root, bd=1, relief='solid', bg='white', height=30)
  16.  
  17. content = tk.Frame(_root, bg='white', bd=0)
  18. content.pack_propagate(0)
  19. canvas = tk.Canvas(content, bg='white', bd=0, highlightthickness=0)
  20. scrollbar = tk.Scrollbar(_root, orient="vertical", command=canvas.yview, troughcolor='white', width=12, bd=0)
  21. dfcontainer = tk.Frame(canvas, bd=0, bg='white')
  22. dirframe = tk.Frame(dfcontainer, bd=0, bg='white')
  23. dcwindow = None
  24. def updatecanvas(e):
  25. global canvas, scrollbar, content, dirframe, dcwindow
  26. try:
  27. canvas.itemconfigure(dcwindow, width=canvas.winfo_width())
  28. ncheight = dirframe.winfo_children()[0].winfo_height() * len(dirframe.winfo_children())
  29. if ncheight < canvas.winfo_height():
  30. canvas.itemconfigure(dcwindow, height=canvas.winfo_height())
  31. else:
  32. canvas.itemconfigure(dcwindow, height=ncheight)
  33. canvas.configure(scrollregion=canvas.bbox("all"))
  34. canvas.update()
  35. scrollbar.update()
  36. except IndexError:
  37. pass
  38. canvas.configure(yscrollcommand=scrollbar.set)
  39. def _on_mousewheel(event):
  40. global time
  41. ysi = 1
  42. for i in range(3):
  43. canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
  44. ysi += 1
  45. canvas.config(yscrollincrement=ysi)
  46. root.update()
  47. time.sleep(0.01)
  48. for i in range(4):
  49. canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
  50. root.update()
  51. time.sleep(0.01)
  52. for i in range(3):
  53. canvas.yview_scroll(int(-1 * (event.delta / 120)), "units")
  54. ysi -= 1
  55. canvas.config(yscrollincrement=ysi)
  56. root.update()
  57. time.sleep(0.01)
  58. canvas.config(yscrollincrement=1)
  59. def mswthread(e):
  60. global Thread, _on_mousewheel
  61. Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start()
  62. canvas.bind("<MouseWheel>", mswthread)
  63. topbar.pack(side='top', fill='x')
  64. content.pack(side='left', expand=True, fill='both')
  65. canvas.pack(side='left', expand=True, fill='both')
  66. _root.update_idletasks()
  67. dcwindow = canvas.create_window((0, 0), window=dfcontainer, anchor="nw", width=canvas.winfo_width())
  68. dirframe.pack(fill='x')
  69. scrollbar.pack(side="left", fill="y", anchor='e')
  70. scrollbar.update()
  71. for child in canvas.winfo_children():
  72. child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  73. dirframe.bind("<Configure>", updatecanvas)
  74. canvas.bind("<Configure>", updatecanvas)
  75.  
  76. global CURRENT_PATH
  77. CURRENT_PATH = 'T:\\Storage'
  78.  
  79. SYSTEM_DIRECTORIES = [
  80. r"T:\BIOS Programs",
  81. r"T:\TechOS",
  82. r"T:\ProgramData",
  83. r"T:\ProgramData\Info",
  84. r"T:\TechOS\Virtual",
  85. r"T:\TechOS\Virtual\Images",
  86. r"T:\TechOS\Virtual\Images\Wallpapers",
  87. r"T:\TechOS\Virtual\Info",
  88. r"T:\TechOS\Virtual\Settings",
  89. r"T:\TechOS\Virtual\Cursors"
  90. ]
  91.  
  92. def goback():
  93. prvpath = os.path.dirname(CURRENT_PATH)
  94. global directory
  95. directory(prvpath)
  96.  
  97. back = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', font='TkDefaultFont 20', width=30, height=30, command=goback)
  98. back.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Back.png')
  99. back.config(image=back.image)
  100. back.pack(side='left', anchor='w', fill='y')
  101.  
  102. dirlabel = tk.Label(topbar, bg='white', text=CURRENT_PATH)
  103. dirlabel.pack(side='left', anchor='w', fill='y')
  104.  
  105. def rename(path):
  106. global SYSTEM_DIRECTORIES
  107. global os
  108. global mb
  109. global spd
  110. global CURRENT_PATH
  111. if any(i in path for i in SYSTEM_DIRECTORIES):
  112. mb.showinfo("Permission Denied", "You do not have permission to rename this file.")
  113. else:
  114. newfilename = spd.askstring("Rename file", "Rename \"" + os.path.basename(path) + "\":")
  115. if newfilename == None:
  116. pass
  117. else:
  118. path0 = os.path.dirname(path)
  119. try:
  120. os.rename(path, os.path.join(path0, newfilename))
  121. except:
  122. mb.showinfo("Error", "Could not rename file")
  123. directory(CURRENT_PATH)
  124.  
  125. def delete(path):
  126. global SYSTEM_DIRECTORIES
  127. global os
  128. global mb
  129. global CURRENT_PATH
  130. global directory
  131. global shutil
  132. if any(i in path for i in SYSTEM_DIRECTORIES):
  133. mb.showinfo("Permission Denied", "You do not have permission to delete this file.")
  134. else:
  135. if os.path.isfile(path):
  136. try:
  137. os.remove(path)
  138. except:
  139. mb.showinfo("Error", "Could not delete file")
  140. elif os.path.isdir(path):
  141. continuedelete = mb.askyesno("Delete Folder", "Are you sure you want to delete this folder and all its files?")
  142. if continuedelete == True:
  143. try:
  144. shutil.rmtree(path)
  145. except:
  146. mb.showinfo("Error", "Could not delete folder")
  147. else:
  148. pass
  149. directory(CURRENT_PATH)
  150.  
  151. def newfile():
  152. global CURRENT_PATH
  153. global directory
  154. filepath1 = os.path.join(CURRENT_PATH, 'Untitled')
  155. rep = 0
  156. while True:
  157. if os.path.exists(filepath1 + str(rep) + '.txt'):
  158. rep += 1
  159. else:
  160. filepath0 = filepath1 + str(rep) + '.txt'
  161. break
  162. NEW_FILE = open(filepath0, 'x')
  163. NEW_FILE.close()
  164. directory(CURRENT_PATH)
  165.  
  166. def newfolder():
  167. global CURRENT_PATH
  168. global directory
  169. folderpath1 = os.path.join(CURRENT_PATH, 'New Folder')
  170. if os.path.exists(folderpath1):
  171. rep = 1
  172. while True:
  173. if os.path.exists(folderpath1 + ' (' + str(rep) + ')'):
  174. rep += 1
  175. else:
  176. folderpath0 = folderpath1 + ' (' + str(rep) + ')'
  177. break
  178. os.mkdir(folderpath0)
  179. else:
  180. os.mkdir(folderpath1)
  181. directory(CURRENT_PATH)
  182.  
  183. newfilebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ File ', command=newfile)
  184. newfilebutton.pack(side='right', anchor='e', fill='y')
  185.  
  186. newfolderbutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ Folder', command=newfolder)
  187. newfolderbutton.pack(side='right', anchor='e', fill='y')
  188.  
  189. def directory(path):
  190. global spd
  191. global mb
  192. global dirframe
  193. global dirlabel
  194. global CURRENT_PATH
  195. global _root
  196. global SYSTEM_DIRECTORIES
  197. global rename
  198. global delete
  199. global tk
  200. global updatecanvas
  201. global goback
  202. if any(path == i for i in SYSTEM_DIRECTORIES):
  203. mb.showinfo("Careful!", "The directory you are about to enter contains system files. Modifying these files might cause irreparable damage to your system.")
  204. CURRENT_PATH = path
  205. dirlabel.config(text=path)
  206. for child in dirframe.winfo_children():
  207. child.destroy()
  208. try:
  209. fullcontents = os.listdir(path)
  210. 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}')]
  211. for element in contents:
  212. fullpath = os.path.join(path, element)
  213. if os.path.isdir(fullpath):
  214. 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)
  215. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Folder.png')
  216. tempbutton.config(image=tempbutton.image)
  217. tempbutton.pack(fill='x', anchor='w', side='top')
  218. tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  219. tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  220. elif os.path.isfile(fullpath):
  221. 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)
  222. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/File.png')
  223. tempbutton.config(image=tempbutton.image)
  224. tempbutton.pack(fill='x', anchor='w', side='top')
  225. tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  226. tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  227. for child in dirframe.winfo_children():
  228. child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  229. updatecanvas("")
  230. except OSError as e:
  231. if e.errno == 13:
  232. mb.showinfo("Error", "Permission Denied")
  233. goback()
  234. elif e.errno == 2:
  235. mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  236. goback()
  237. else:
  238. mb.showinfo("Error", "An unknown error has occurred.")
  239.  
  240. def directory_reload(e):
  241. global CURRENT_PATH, directory
  242. directory(CURRENT_PATH)
  243.  
  244. def directory_rename():
  245. global spd
  246. global mb
  247. global dirframe
  248. global dirlabel
  249. global CURRENT_PATH
  250. global _root
  251. global SYSTEM_DIRECTORIES
  252. global rename
  253. global delete
  254. global tk
  255. global directory
  256. global updatecanvas
  257. global goback
  258. path = CURRENT_PATH
  259. dirlabel.config(text=path)
  260. for child in dirframe.winfo_children():
  261. child.destroy()
  262. try:
  263. fullcontents = os.listdir(path)
  264. 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}')]
  265. for element in contents:
  266. fullpath = os.path.join(path, element)
  267. if os.path.isdir(fullpath):
  268. 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)
  269. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderRename.png')
  270. tempbutton.config(image=tempbutton.image)
  271. tempbutton.pack(fill='x', anchor='w', side='top')
  272. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  273. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  274. elif os.path.isfile(fullpath):
  275. 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)
  276. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileRename.png')
  277. tempbutton.config(image=tempbutton.image)
  278. tempbutton.pack(fill='x', anchor='w', side='top')
  279. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  280. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  281. for child in dirframe.winfo_children():
  282. child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  283. updatecanvas("")
  284. except OSError as e:
  285. if e.errno == 13:
  286. mb.showinfo("Error", "Permission Denied")
  287. goback()
  288. elif e.errno == 2:
  289. mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  290. goback()
  291. else:
  292. mb.showinfo("Error", "An unknown error has occurred.")
  293.  
  294. def directory_delete():
  295. global spd
  296. global mb
  297. global dirframe
  298. global dirlabel
  299. global CURRENT_PATH
  300. global _root
  301. global SYSTEM_DIRECTORIES
  302. global rename
  303. global delete
  304. global tk
  305. global directory
  306. global updatecanvas
  307. global goback
  308. path = CURRENT_PATH
  309. dirlabel.config(text=path)
  310. for child in dirframe.winfo_children():
  311. child.destroy()
  312. try:
  313. fullcontents = os.listdir(path)
  314. 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}')]
  315. for element in contents:
  316. fullpath = os.path.join(path, element)
  317. if os.path.isdir(fullpath):
  318. 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)
  319. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderDelete.png')
  320. tempbutton.config(image=tempbutton.image)
  321. tempbutton.pack(fill='x', anchor='w', side='top')
  322. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  323. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  324. elif os.path.isfile(fullpath):
  325. 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)
  326. tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileDelete.png')
  327. tempbutton.config(image=tempbutton.image)
  328. tempbutton.pack(fill='x', anchor='w', side='top')
  329. tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  330. tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  331. for child in dirframe.winfo_children():
  332. child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  333. updatecanvas("")
  334. except OSError as e:
  335. if e.errno == 13:
  336. mb.showinfo("Error", "Permission Denied")
  337. goback()
  338. elif e.errno == 2:
  339. mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  340. goback()
  341. else:
  342. mb.showinfo("Error", "An unknown error has occurred.")
  343.  
  344. deletebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Delete', command=directory_delete)
  345. deletebutton.pack(side='right', anchor='e', fill='y')
  346.  
  347. renamebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Rename', command=directory_rename)
  348. renamebutton.pack(side='right', anchor='e', fill='y')
  349.  
  350. dfcontainer.bind("<Button>", directory_reload)
  351.  
  352. directory(CURRENT_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement