Advertisement
Najeebsk

LIST-EXE.pyw

Jun 7th, 2024 (edited)
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.99 KB | None | 0 0
  1. import os
  2. import subprocess
  3. import tkinter as tk
  4. from tkinter import messagebox, ttk
  5. from ttkthemes import ThemedTk
  6.  
  7. def read_file_list(file_path):
  8.     """Read the file list from List.txt and return a dictionary of display names and file paths."""
  9.     file_dict = {}
  10.     try:
  11.         with open(file_path, 'r') as file:
  12.             for line in file:
  13.                 if ';' in line:
  14.                     display_name, file_path = line.strip().split(' ; ', 1)
  15.                     file_dict[display_name] = file_path
  16.     except Exception as e:
  17.         messagebox.showerror("Error", f"Failed to read List.txt: {e}")
  18.     return file_dict
  19.  
  20. def open_file(filepath):
  21.     """Open the selected file."""
  22.     try:
  23.         os.startfile(filepath)
  24.     except Exception as e:
  25.         messagebox.showerror("Error", f"Failed to open file: {e}")
  26.  
  27. def on_file_select(event):
  28.     """Event handler for file selection."""
  29.     selected_index = file_listbox.curselection()
  30.     if selected_index:
  31.         selected_display_name = file_listbox.get(selected_index)
  32.         selected_file_path = file_dict.get(selected_display_name)
  33.         if selected_file_path:
  34.             open_file(selected_file_path)
  35.  
  36. def update_listbox(filter_text=""):
  37.     """Update the listbox based on the search filter."""
  38.     file_listbox.delete(0, tk.END)
  39.     for display_name in file_dict.keys():
  40.         if filter_text.lower() in display_name.lower():
  41.             file_listbox.insert(tk.END, display_name)
  42.  
  43. def on_search_change(event):
  44.     """Event handler for search field change."""
  45.     filter_text = search_var.get()
  46.     update_listbox(filter_text)
  47.  
  48. # Determine the directory where the script is located
  49. script_dir = os.path.dirname(os.path.abspath(__file__))
  50.  
  51. # Path to the List.txt file
  52. list_file_path = os.path.join(script_dir, 'LIST-EXE.txt')
  53.  
  54. # Read the file paths from List.txt
  55. file_dict = read_file_list(list_file_path)
  56.  
  57. # Create the main window with a theme
  58. root = ThemedTk(theme="arc")
  59. root.title("Najeeb Select a File to Open")
  60. root.geometry("320x360")
  61. root.configure(bg="#4a4a4a")
  62.  
  63. # Create a frame for better layout control
  64. frame = ttk.Frame(root, padding="10")
  65. frame.pack(fill=tk.BOTH, expand=True)
  66.  
  67. # Create a search entry field
  68. search_var = tk.StringVar()
  69. search_entry = ttk.Entry(frame, textvariable=search_var)
  70. search_entry.pack(fill=tk.X, padx=(0, 5))
  71. search_entry.bind('<KeyRelease>', on_search_change)
  72.  
  73. # Create a Listbox to display the display names
  74. file_listbox = tk.Listbox(frame, selectmode=tk.SINGLE, height=15)
  75. file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
  76.  
  77. # Create a scrollbar for the Listbox
  78. scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=file_listbox.yview)
  79. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  80. file_listbox.config(yscrollcommand=scrollbar.set)
  81.  
  82. # Bind the selection event to the handler
  83. file_listbox.bind('<<ListboxSelect>>', on_file_select)
  84.  
  85. # Initialize the listbox with all items
  86. update_listbox()
  87.  
  88. # Start the GUI event loop
  89. root.mainloop()
  90.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement