Advertisement
Najeebsk

OPEN-TOOL.pyw

Jun 2nd, 2025
559
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.59 KB | None | 0 0
  1. import tkinter as tk
  2. from tkinter import messagebox, Scrollbar, Listbox, Entry, Label, Button, Frame, Toplevel, StringVar, END, BOTH, LEFT, RIGHT, X, Y, TOP, W, E, S
  3. from pypinyin import Style
  4. import pypinyin
  5. import webbrowser
  6. import json
  7. #================ADD-IMAGE-ICON=================
  8. import os
  9. import sys
  10.  
  11. def resource_path(relative_path):
  12.     """ Get the absolute path to the resource, works for PyInstaller. """
  13.     if getattr(sys, '_MEIPASS', False):
  14.         return os.path.join(sys._MEIPASS, relative_path)
  15.     return os.path.join(os.path.abspath("."), relative_path)
  16.  
  17. # Use this function to load files:
  18. splash_image = resource_path("splash-1.png")
  19. icon_path = resource_path("opentool.ico")
  20. #================ADD-IMAGE-ICON=================
  21. # ==================ADD-SPLASH==================
  22. import tkinter as tk
  23. from PIL import Image, ImageTk
  24. import time
  25.  
  26.  
  27. def show_splash(image_path):
  28.     # Create splash screen window
  29.     splash = tk.Tk()
  30.     splash.overrideredirect(True)  # Remove window border
  31.    
  32.     # Load the image
  33.     image = Image.open(image_path)
  34.     img = ImageTk.PhotoImage(image)
  35.  
  36.     # Get image dimensions
  37.     img_width, img_height = image.size
  38.  
  39.     # Calculate position to center the splash screen
  40.     screen_width = splash.winfo_screenwidth()
  41.     screen_height = splash.winfo_screenheight()
  42.     x = (screen_width - img_width) // 2
  43.     y = (screen_height - img_height) // 2
  44.     splash.geometry(f"{img_width}x{img_height}+{x}+{y}")
  45.  
  46.     # Set transparent background
  47.     splash.config(bg="white")
  48.     splash.attributes("-transparentcolor", "white")  # Make white color transparent
  49.  
  50.     # Display the image
  51.     label = tk.Label(splash, image=img, bg="white")
  52.     label.pack()
  53.  
  54.     # Display the splash screen for 5 seconds
  55.     splash.after(5000, splash.destroy)
  56.     splash.mainloop()
  57.  
  58.  
  59. # Call splash screen function
  60. show_splash(splash_image) # Replace with your image file path 1 To 6
  61.  
  62. # ==================ADD-SPLASH==================
  63. FONT_1 = ('Microsoft YaHei', 14, 'normal')
  64. FONT_2 = ('Arial', 12, 'normal')
  65.  
  66.  
  67. # Popup window for Add/Edit
  68. class PopupDialog(tk.Toplevel):
  69.     def __init__(self, parent, existing_name=None, existing_url=None):
  70.         super().__init__()
  71.         sw = self.winfo_screenwidth()
  72.         sh = self.winfo_screenheight() - 100
  73.         ww = 400
  74.         wh = 110
  75.         x = (sw - ww) / 2
  76.         y = (sh - wh) / 2
  77.         self.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
  78.         self.resizable(0, 0)
  79.  
  80.         self.title('Edit Path' if existing_name else 'Add Path')
  81.         self.transient(parent)
  82.         self.parent = parent
  83.         frame = Frame(self)
  84.         frame.grid()
  85.  
  86.         self.grab_set()
  87.         self.protocol("WM_DELETE_WINDOW", self.cancel)
  88.  
  89.         self.name = StringVar(value=existing_name if existing_name else "")
  90.         self.url = StringVar(value=existing_url if existing_url else "")
  91.         self.existing_name = existing_name
  92.  
  93.         # Row 1: Name
  94.         Label(frame, text='Name:').grid(row=0, column=0, sticky=E, pady=8)
  95.         self.entry1 = Entry(frame, textvariable=self.name, width=50)
  96.         self.entry1.grid(row=0, column=1, columnspan=4, sticky=W, pady=8)
  97.  
  98.         # Row 2: Path
  99.         Label(frame, text='Path:').grid(row=1, column=0, sticky=E)
  100.         Entry(frame, textvariable=self.url, width=50).grid(row=1, column=1, columnspan=4, sticky=W)
  101.  
  102.         # Row 3: Buttons
  103.         Button(frame, text="OK", command=self.ok).grid(row=2, column=2, sticky=S, pady=10)
  104.         Button(frame, text="Cancel", command=self.cancel).grid(row=2, column=3, sticky=S, pady=10)
  105.  
  106.         self.entry1.focus_set()
  107.         self.bind("<Return>", self.ok)
  108.         self.bind("<Escape>", self.cancel)
  109.  
  110.     def ok(self, event=None):
  111.         new_name = self.name.get().strip()
  112.         new_url = self.url.get().strip()
  113.  
  114.         if not new_name or not new_url:
  115.             messagebox.showwarning('Warning', 'Input cannot be empty!')
  116.             return
  117.  
  118.         if self.existing_name and new_name != self.existing_name:
  119.             if new_name in self.parent.urllist:
  120.                 if not messagebox.askyesno('Prompt', f'Name “{new_name}” already exists. Overwrite it?'):
  121.                     return
  122.             del self.parent.urllist[self.existing_name]
  123.  
  124.         self.parent.urllist[new_name] = new_url
  125.  
  126.         self.parent.listbox.delete(0, END)
  127.         for item in self.parent.urllist:
  128.             self.parent.listbox.insert(END, item)
  129.         self.destroy()
  130.  
  131.     def cancel(self, event=None):
  132.         self.destroy()
  133.  
  134.  
  135. # Main application window
  136. class Application(tk.Frame):
  137.     def __init__(self, master=None):
  138.         super().__init__()
  139.         self.master = master
  140.         self.pack()
  141.  
  142.         self.urllist = self.readUrlList()
  143.         if self.urllist:
  144.             self.createWidgets()
  145.             self.mainloop()
  146.         else:
  147.             messagebox.showinfo('Error', 'Failed to load URL list! Please check if openlist.json exists and is formatted correctly.')
  148.  
  149.     def readUrlList(self):
  150.         try:
  151.             with open('openlist.json', 'r', encoding='utf-8') as f_obj:
  152.                 return json.load(f_obj)
  153.         except FileNotFoundError:
  154.             return {}
  155.  
  156.     def saveUrllist(self):
  157.         with open('openlist.json', 'w', encoding='utf-8') as f:
  158.             json.dump(self.urllist, f, ensure_ascii=False, indent=2)
  159.         print('File saved successfully.')
  160.  
  161.     def createWidgets(self):
  162.         # Search bar
  163.         self.frame1 = Frame()
  164.         self.frame1.pack(side=TOP, fill=X)
  165.         Label(self.frame1, text='Search:', font=FONT_2).pack(side=LEFT)
  166.         self.keywdbox = Entry(self.frame1, font=FONT_2)
  167.         self.keywdbox.pack(side=LEFT, fill=X, expand=True)
  168.         self.keywdbox.focus_set()
  169.  
  170.         # List box with scrollbar
  171.         self.frame2 = Frame()
  172.         self.frame2.pack(side=TOP, fill=X, pady=5)
  173.         self.scrolly = Scrollbar(self.frame2)
  174.         self.scrolly.pack(side=RIGHT, fill=Y)
  175.         self.listbox = Listbox(self.frame2, width=60, height=15, font=FONT_1, yscrollcommand=self.scrolly.set)
  176.         self.listbox.pack(fill=BOTH, expand=True)
  177.         self.scrolly.config(command=self.listbox.yview)
  178.  
  179.         # Button controls
  180.         self.frame3 = Frame(root, bg="#2c3e50")
  181.         self.frame3.pack(side=TOP, fill=BOTH, pady=10)
  182.         #Label(self.frame3, width=10).pack(side=LEFT)
  183.         Button(self.frame3, text='Add', width=10, command=self.additem).pack(side=LEFT, padx=10)
  184.         Button(self.frame3, text='Edit', width=10, command=self.edititem).pack(side=LEFT, padx=10)
  185.         Button(self.frame3, text='Delete', width=10, command=self.deleteitem).pack(side=LEFT, padx=10)
  186.         #Label(self.frame3, width=10).pack(side=RIGHT)
  187.  
  188.         # Populate list
  189.         for item in self.urllist:
  190.             self.listbox.insert(END, item)
  191.  
  192.         self.doevent()
  193.  
  194.     def doevent(self):
  195.         self.keywdbox.bind("<KeyRelease>", self.showlist)
  196.         self.listbox.bind('<Double-Button-1>', self.openurl)
  197.         self.listbox.bind('<Return>', self.openurl)
  198.         self.listbox.bind('<Left>', lambda e: self.keywdbox.focus_set())
  199.         self.keywdbox.bind("<Down>", self.jump_to_listbox)
  200.  
  201.     def additem(self):
  202.         pw = PopupDialog(self)
  203.         self.wait_window(pw)
  204.         self.saveUrllist()
  205.  
  206.     def edititem(self):
  207.         index = self.listbox.curselection()
  208.         if not index:
  209.             messagebox.showinfo('Info', 'Please select an item to edit!')
  210.             return
  211.  
  212.         name = self.listbox.get(index)
  213.         url = self.urllist.get(name, "")
  214.         pw = PopupDialog(self, existing_name=name, existing_url=url)
  215.         self.wait_window(pw)
  216.         self.saveUrllist()
  217.  
  218.     def deleteitem(self):
  219.         index = self.listbox.curselection()
  220.         if not index:
  221.             messagebox.showinfo('Info', 'Please select an item to delete!')
  222.             return
  223.  
  224.         item = self.listbox.get(index)
  225.         if messagebox.askyesno('Delete', f'Delete {item}?'):
  226.             self.listbox.delete(index)
  227.             del self.urllist[item]
  228.             self.saveUrllist()
  229.             messagebox.showinfo('Info', 'Deleted successfully')
  230.  
  231.     def openurl(self, event):
  232.         index = self.listbox.curselection()
  233.         if not index:
  234.             return
  235.         urlname = self.listbox.get(index)
  236.         url = self.urllist.get(urlname)
  237.         if url:
  238.             webbrowser.open(url)
  239.         else:
  240.             messagebox.showinfo('Error', 'Failed to open. URL is empty.')
  241.  
  242.     def jump_to_listbox(self, event):
  243.         if self.listbox.size():
  244.             self.listbox.select_clear(0, END)
  245.             self.listbox.select_set(0)
  246.             self.listbox.activate(0)
  247.             self.listbox.focus_set()
  248.  
  249.     def showlist(self, event):
  250.         keywd = self.keywdbox.get().strip()
  251.         self.listbox.delete(0, END)
  252.         if keywd:
  253.             for item in self.urllist:
  254.                 cond_1 = keywd.lower() in item.lower()
  255.                 cond_2 = keywd.lower() in pypinyin.slug(item.lower(), separator='')
  256.                 cond_3 = keywd.lower() in pypinyin.slug(item.lower(), style=Style.FIRST_LETTER, separator='')
  257.                 if any([cond_1, cond_2, cond_3]):
  258.                     self.listbox.insert(END, item)
  259.         else:
  260.             for item in self.urllist:
  261.                 self.listbox.insert(END, item)
  262.  
  263.  
  264. if __name__ == '__main__':
  265.     root = tk.Tk()
  266.     root.title('Najeeb Shah Khan Open Everything')
  267.     root.configure(bg="#2c3e50")
  268.    
  269.     try:
  270.         root.iconbitmap(icon_path)
  271.     except:
  272.         pass
  273.     root.resizable(0, 0)
  274.     app = Application(master=root)
  275.     if app.urllist:
  276.         app.saveUrllist()
  277.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement