Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import tkinter as tk
- from tkinter import messagebox, Scrollbar, Listbox, Entry, Label, Button, Frame, Toplevel, StringVar, END, BOTH, LEFT, RIGHT, X, Y, TOP, W, E, S
- from pypinyin import Style
- import pypinyin
- import webbrowser
- import json
- #================ADD-IMAGE-ICON=================
- import os
- import sys
- def resource_path(relative_path):
- """ Get the absolute path to the resource, works for PyInstaller. """
- if getattr(sys, '_MEIPASS', False):
- return os.path.join(sys._MEIPASS, relative_path)
- return os.path.join(os.path.abspath("."), relative_path)
- # Use this function to load files:
- splash_image = resource_path("splash-1.png")
- icon_path = resource_path("opentool.ico")
- #================ADD-IMAGE-ICON=================
- # ==================ADD-SPLASH==================
- import tkinter as tk
- from PIL import Image, ImageTk
- import time
- def show_splash(image_path):
- # Create splash screen window
- splash = tk.Tk()
- splash.overrideredirect(True) # Remove window border
- # Load the image
- image = Image.open(image_path)
- img = ImageTk.PhotoImage(image)
- # Get image dimensions
- img_width, img_height = image.size
- # Calculate position to center the splash screen
- screen_width = splash.winfo_screenwidth()
- screen_height = splash.winfo_screenheight()
- x = (screen_width - img_width) // 2
- y = (screen_height - img_height) // 2
- splash.geometry(f"{img_width}x{img_height}+{x}+{y}")
- # Set transparent background
- splash.config(bg="white")
- splash.attributes("-transparentcolor", "white") # Make white color transparent
- # Display the image
- label = tk.Label(splash, image=img, bg="white")
- label.pack()
- # Display the splash screen for 5 seconds
- splash.after(5000, splash.destroy)
- splash.mainloop()
- # Call splash screen function
- show_splash(splash_image) # Replace with your image file path 1 To 6
- # ==================ADD-SPLASH==================
- FONT_1 = ('Microsoft YaHei', 14, 'normal')
- FONT_2 = ('Arial', 12, 'normal')
- # Popup window for Add/Edit
- class PopupDialog(tk.Toplevel):
- def __init__(self, parent, existing_name=None, existing_url=None):
- super().__init__()
- sw = self.winfo_screenwidth()
- sh = self.winfo_screenheight() - 100
- ww = 400
- wh = 110
- x = (sw - ww) / 2
- y = (sh - wh) / 2
- self.geometry("%dx%d+%d+%d" % (ww, wh, x, y))
- self.resizable(0, 0)
- self.title('Edit Path' if existing_name else 'Add Path')
- self.transient(parent)
- self.parent = parent
- frame = Frame(self)
- frame.grid()
- self.grab_set()
- self.protocol("WM_DELETE_WINDOW", self.cancel)
- self.name = StringVar(value=existing_name if existing_name else "")
- self.url = StringVar(value=existing_url if existing_url else "")
- self.existing_name = existing_name
- # Row 1: Name
- Label(frame, text='Name:').grid(row=0, column=0, sticky=E, pady=8)
- self.entry1 = Entry(frame, textvariable=self.name, width=50)
- self.entry1.grid(row=0, column=1, columnspan=4, sticky=W, pady=8)
- # Row 2: Path
- Label(frame, text='Path:').grid(row=1, column=0, sticky=E)
- Entry(frame, textvariable=self.url, width=50).grid(row=1, column=1, columnspan=4, sticky=W)
- # Row 3: Buttons
- Button(frame, text="OK", command=self.ok).grid(row=2, column=2, sticky=S, pady=10)
- Button(frame, text="Cancel", command=self.cancel).grid(row=2, column=3, sticky=S, pady=10)
- self.entry1.focus_set()
- self.bind("<Return>", self.ok)
- self.bind("<Escape>", self.cancel)
- def ok(self, event=None):
- new_name = self.name.get().strip()
- new_url = self.url.get().strip()
- if not new_name or not new_url:
- messagebox.showwarning('Warning', 'Input cannot be empty!')
- return
- if self.existing_name and new_name != self.existing_name:
- if new_name in self.parent.urllist:
- if not messagebox.askyesno('Prompt', f'Name “{new_name}” already exists. Overwrite it?'):
- return
- del self.parent.urllist[self.existing_name]
- self.parent.urllist[new_name] = new_url
- self.parent.listbox.delete(0, END)
- for item in self.parent.urllist:
- self.parent.listbox.insert(END, item)
- self.destroy()
- def cancel(self, event=None):
- self.destroy()
- # Main application window
- class Application(tk.Frame):
- def __init__(self, master=None):
- super().__init__()
- self.master = master
- self.pack()
- self.urllist = self.readUrlList()
- if self.urllist:
- self.createWidgets()
- self.mainloop()
- else:
- messagebox.showinfo('Error', 'Failed to load URL list! Please check if openlist.json exists and is formatted correctly.')
- def readUrlList(self):
- try:
- with open('openlist.json', 'r', encoding='utf-8') as f_obj:
- return json.load(f_obj)
- except FileNotFoundError:
- return {}
- def saveUrllist(self):
- with open('openlist.json', 'w', encoding='utf-8') as f:
- json.dump(self.urllist, f, ensure_ascii=False, indent=2)
- print('File saved successfully.')
- def createWidgets(self):
- # Search bar
- self.frame1 = Frame()
- self.frame1.pack(side=TOP, fill=X)
- Label(self.frame1, text='Search:', font=FONT_2).pack(side=LEFT)
- self.keywdbox = Entry(self.frame1, font=FONT_2)
- self.keywdbox.pack(side=LEFT, fill=X, expand=True)
- self.keywdbox.focus_set()
- # List box with scrollbar
- self.frame2 = Frame()
- self.frame2.pack(side=TOP, fill=X, pady=5)
- self.scrolly = Scrollbar(self.frame2)
- self.scrolly.pack(side=RIGHT, fill=Y)
- self.listbox = Listbox(self.frame2, width=60, height=15, font=FONT_1, yscrollcommand=self.scrolly.set)
- self.listbox.pack(fill=BOTH, expand=True)
- self.scrolly.config(command=self.listbox.yview)
- # Button controls
- self.frame3 = Frame(root, bg="#2c3e50")
- self.frame3.pack(side=TOP, fill=BOTH, pady=10)
- #Label(self.frame3, width=10).pack(side=LEFT)
- Button(self.frame3, text='Add', width=10, command=self.additem).pack(side=LEFT, padx=10)
- Button(self.frame3, text='Edit', width=10, command=self.edititem).pack(side=LEFT, padx=10)
- Button(self.frame3, text='Delete', width=10, command=self.deleteitem).pack(side=LEFT, padx=10)
- #Label(self.frame3, width=10).pack(side=RIGHT)
- # Populate list
- for item in self.urllist:
- self.listbox.insert(END, item)
- self.doevent()
- def doevent(self):
- self.keywdbox.bind("<KeyRelease>", self.showlist)
- self.listbox.bind('<Double-Button-1>', self.openurl)
- self.listbox.bind('<Return>', self.openurl)
- self.listbox.bind('<Left>', lambda e: self.keywdbox.focus_set())
- self.keywdbox.bind("<Down>", self.jump_to_listbox)
- def additem(self):
- pw = PopupDialog(self)
- self.wait_window(pw)
- self.saveUrllist()
- def edititem(self):
- index = self.listbox.curselection()
- if not index:
- messagebox.showinfo('Info', 'Please select an item to edit!')
- return
- name = self.listbox.get(index)
- url = self.urllist.get(name, "")
- pw = PopupDialog(self, existing_name=name, existing_url=url)
- self.wait_window(pw)
- self.saveUrllist()
- def deleteitem(self):
- index = self.listbox.curselection()
- if not index:
- messagebox.showinfo('Info', 'Please select an item to delete!')
- return
- item = self.listbox.get(index)
- if messagebox.askyesno('Delete', f'Delete {item}?'):
- self.listbox.delete(index)
- del self.urllist[item]
- self.saveUrllist()
- messagebox.showinfo('Info', 'Deleted successfully')
- def openurl(self, event):
- index = self.listbox.curselection()
- if not index:
- return
- urlname = self.listbox.get(index)
- url = self.urllist.get(urlname)
- if url:
- webbrowser.open(url)
- else:
- messagebox.showinfo('Error', 'Failed to open. URL is empty.')
- def jump_to_listbox(self, event):
- if self.listbox.size():
- self.listbox.select_clear(0, END)
- self.listbox.select_set(0)
- self.listbox.activate(0)
- self.listbox.focus_set()
- def showlist(self, event):
- keywd = self.keywdbox.get().strip()
- self.listbox.delete(0, END)
- if keywd:
- for item in self.urllist:
- cond_1 = keywd.lower() in item.lower()
- cond_2 = keywd.lower() in pypinyin.slug(item.lower(), separator='')
- cond_3 = keywd.lower() in pypinyin.slug(item.lower(), style=Style.FIRST_LETTER, separator='')
- if any([cond_1, cond_2, cond_3]):
- self.listbox.insert(END, item)
- else:
- for item in self.urllist:
- self.listbox.insert(END, item)
- if __name__ == '__main__':
- root = tk.Tk()
- root.title('Najeeb Shah Khan Open Everything')
- root.configure(bg="#2c3e50")
- try:
- root.iconbitmap(icon_path)
- except:
- pass
- root.resizable(0, 0)
- app = Application(master=root)
- if app.urllist:
- app.saveUrllist()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement