Techpad

ExternalFiles.tep

Mar 11th, 2021 (edited)
231
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 14.32 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:\\'
  78.  
  79. def goback():
  80.     prvpath = os.path.dirname(CURRENT_PATH)
  81.     global directory
  82.     directory(prvpath)
  83.  
  84. def gotopath(e):
  85.     global spd
  86.     global directory
  87.     global mb
  88.     newpath = spd.askstring("Directory Jump", "Type path of directory:")
  89.     if os.path.isdir(newpath):
  90.         directory(newpath)
  91.     else:
  92.         mb.showinfo("Invalid path", "The path you specified is not a directory. Please try again.")
  93.  
  94. back = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', font='TkDefaultFont 20', width=30, height=30, command=goback)
  95. back.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Back.png')
  96. back.config(image=back.image)
  97. back.pack(side='left', anchor='w', fill='y')
  98.  
  99. dirlabel = tk.Label(topbar, bg='white', text=CURRENT_PATH)
  100. dirlabel.pack(side='left', anchor='w', fill='y')
  101.  
  102. dirlabel.bind("<Button-1>", gotopath)
  103.  
  104. def rename(path):
  105.     global os
  106.     global mb
  107.     global spd
  108.     global CURRENT_PATH
  109.     newfilename = spd.askstring("Rename file", "Rename \"" + os.path.basename(path) + "\":")
  110.     if newfilename == None:
  111.         pass
  112.     else:
  113.         path0 = os.path.dirname(path)
  114.         try:
  115.             os.rename(path, os.path.join(path0, newfilename))
  116.             directory(CURRENT_PATH)
  117.         except:
  118.             mb.showinfo("Error", "Could not rename file")
  119.  
  120. def delete(path):
  121.     global os
  122.     global mb
  123.     global CURRENT_PATH
  124.     global directory
  125.     global shutil
  126.     if os.path.isfile(path):
  127.         try:
  128.             os.remove(path)
  129.         except:
  130.             mb.showinfo("Error", "Could not delete file")
  131.     elif os.path.isdir(path):
  132.         continuedelete = mb.askyesno("Delete Folder", "Are you sure you want to delete this folder and all its files?")
  133.         if continuedelete == True:
  134.             try:
  135.                 shutil.rmtree(path)
  136.             except:
  137.                 mb.showinfo("Error", "Could not delete folder")
  138.         else:
  139.             pass
  140.     directory(CURRENT_PATH)
  141.  
  142. def newfile():
  143.     global CURRENT_PATH
  144.     global directory
  145.     filepath1 = os.path.join(CURRENT_PATH, 'Untitled')
  146.     rep = 0
  147.     while True:
  148.         if os.path.exists(filepath1 + str(rep) + '.txt'):
  149.             rep += 1
  150.         else:
  151.             filepath0 = filepath1 + str(rep) + '.txt'
  152.             break
  153.     NEW_FILE = open(filepath0, 'x')
  154.     NEW_FILE.close()
  155.     directory(CURRENT_PATH)
  156.  
  157. def newfolder():
  158.     global CURRENT_PATH
  159.     global directory
  160.     folderpath1 = os.path.join(CURRENT_PATH, 'New Folder')
  161.     if os.path.exists(folderpath1):
  162.         rep = 1
  163.         while True:
  164.             if os.path.exists(folderpath1 + ' (' + str(rep) + ')'):
  165.                 rep += 1
  166.             else:
  167.                 folderpath0 = folderpath1 + ' (' + str(rep) + ')'
  168.                 break
  169.         os.mkdir(folderpath0)
  170.     else:
  171.         os.mkdir(folderpath1)
  172.     directory(CURRENT_PATH)
  173.  
  174. newfilebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ File ', command=newfile)
  175. newfilebutton.pack(side='right', anchor='e', fill='y')
  176.  
  177. newfolderbutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='+ Folder', command=newfolder)
  178. newfolderbutton.pack(side='right', anchor='e', fill='y')
  179.  
  180. def directory(path):
  181.     global spd
  182.     global mb
  183.     global dirframe
  184.     global dirlabel
  185.     global CURRENT_PATH
  186.     global _root
  187.     global rename
  188.     global delete
  189.     global tk
  190.     global updatecanvas
  191.     global goback
  192.     CURRENT_PATH = path
  193.     dirlabel.config(text=path)
  194.     for child in dirframe.winfo_children():
  195.         child.destroy()
  196.     try:
  197.         fullcontents = os.listdir(path)
  198.         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}')]
  199.         for element in contents:
  200.             fullpath = os.path.join(path, element)
  201.             if os.path.isdir(fullpath):
  202.                 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)
  203.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/Folder.png')
  204.                 tempbutton.config(image=tempbutton.image)
  205.                 tempbutton.pack(fill='x', anchor='w', side='top')
  206.                 tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  207.                 tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  208.             elif os.path.isfile(fullpath):
  209.                 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)
  210.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/File.png')
  211.                 tempbutton.config(image=tempbutton.image)
  212.                 tempbutton.pack(fill='x', anchor='w', side='top')
  213.                 tempbutton.bind("<Button-3>", lambda event, fullpath=fullpath: rename(fullpath))
  214.                 tempbutton.bind("<Button-2>", lambda event, fullpath=fullpath: delete(fullpath))
  215.         for child in dirframe.winfo_children():
  216.             child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  217.         updatecanvas("")
  218.     except OSError as e:
  219.         if e.errno == 13:
  220.             mb.showinfo("Error", "Permission Denied")
  221.             goback()
  222.         elif e.errno == 2:
  223.             mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  224.             goback()
  225.         else:
  226.             mb.showinfo("Error", "An unknown error has occurred.")
  227.  
  228. def directory_reload(e):
  229.     global CURRENT_PATH, directory
  230.     directory(CURRENT_PATH)
  231.  
  232. def directory_rename():
  233.     global spd
  234.     global mb
  235.     global dirframe
  236.     global dirlabel
  237.     global CURRENT_PATH
  238.     global _root
  239.     global SYSTEM_DIRECTORIES
  240.     global rename
  241.     global delete
  242.     global tk
  243.     global directory
  244.     global updatecanvas
  245.     global goback
  246.     path = CURRENT_PATH
  247.     dirlabel.config(text=path)
  248.     for child in dirframe.winfo_children():
  249.         child.destroy()
  250.     try:
  251.         fullcontents = os.listdir(path)
  252.         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}')]
  253.         for element in contents:
  254.             fullpath = os.path.join(path, element)
  255.             if os.path.isdir(fullpath):
  256.                 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)
  257.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderRename.png')
  258.                 tempbutton.config(image=tempbutton.image)
  259.                 tempbutton.pack(fill='x', anchor='w', side='top')
  260.                 tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  261.                 tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  262.             elif os.path.isfile(fullpath):
  263.                 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)
  264.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileRename.png')
  265.                 tempbutton.config(image=tempbutton.image)
  266.                 tempbutton.pack(fill='x', anchor='w', side='top')
  267.                 tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  268.                 tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  269.         for child in dirframe.winfo_children():
  270.             child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  271.         updatecanvas("")
  272.     except OSError as e:
  273.         if e.errno == 13:
  274.             mb.showinfo("Error", "Permission Denied")
  275.             goback()
  276.         elif e.errno == 2:
  277.             mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  278.             goback()
  279.         else:
  280.             mb.showinfo("Error", "An unknown error has occurred.")
  281.  
  282. def directory_delete():
  283.     global spd
  284.     global mb
  285.     global dirframe
  286.     global dirlabel
  287.     global CURRENT_PATH
  288.     global _root
  289.     global SYSTEM_DIRECTORIES
  290.     global rename
  291.     global delete
  292.     global tk
  293.     global directory
  294.     global updatecanvas
  295.     global goback
  296.     path = CURRENT_PATH
  297.     dirlabel.config(text=path)
  298.     for child in dirframe.winfo_children():
  299.         child.destroy()
  300.     try:
  301.         fullcontents = os.listdir(path)
  302.         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}')]
  303.         for element in contents:
  304.             fullpath = os.path.join(path, element)
  305.             if os.path.isdir(fullpath):
  306.                 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)
  307.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FolderDelete.png')
  308.                 tempbutton.config(image=tempbutton.image)
  309.                 tempbutton.pack(fill='x', anchor='w', side='top')
  310.                 tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  311.                 tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  312.             elif os.path.isfile(fullpath):
  313.                 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)
  314.                 tempbutton.image = tk.PhotoImage(file='T:/TechOS/Virtual/Images/FileDelete.png')
  315.                 tempbutton.config(image=tempbutton.image)
  316.                 tempbutton.pack(fill='x', anchor='w', side='top')
  317.                 tempbutton.bind("<Button-3>", lambda event, path=path: directory(path))
  318.                 tempbutton.bind("<Button-2>", lambda event, path=path: directory(path))
  319.         for child in dirframe.winfo_children():
  320.             child.bind("<MouseWheel>", lambda e: Thread(target=lambda e=e: _on_mousewheel(e), daemon=True).start())
  321.         updatecanvas("")
  322.     except OSError as e:
  323.         if e.errno == 13:
  324.             mb.showinfo("Error", "Permission Denied")
  325.             goback()
  326.         elif e.errno == 2:
  327.             mb.showinfo("Error", "The folder you tried to open no longer exists or it is formatted incorrectly.")
  328.             goback()
  329.         else:
  330.             mb.showinfo("Error", "An unknown error has occurred.")
  331.  
  332. with open("T:/TechOS/Virtual/Info/Version.info", "r") as f:
  333.     if int(f.read()) >= 8:
  334.         deletebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Delete', command=directory_delete)
  335.         deletebutton.pack(side='right', anchor='e', fill='y')
  336.  
  337.         renamebutton = tk.Button(topbar, bd=0, bg='white', activebackground='lightgray', text='Rename', command=directory_rename)
  338.         renamebutton.pack(side='right', anchor='e', fill='y')
  339.     else:
  340.         pass
  341.  
  342. dfcontainer.bind("<Button>", directory_reload)
  343.  
  344. directory(CURRENT_PATH)
Add Comment
Please, Sign In to add comment