Advertisement
Techpad

Files 4

Mar 16th, 2021
1,739
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
E 6.45 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.                 directory(CURRENT_PATH)
  65.             except:
  66.                 mb.showinfo("Error", "Could not rename file")
  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. directory(CURRENT_PATH)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement