Advertisement
Najeebsk

T-MENU-EXE.pyw

Jun 7th, 2024
346
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 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 list_files(directory):
  8.     """List all .lnk and .exe files in the specified directory."""
  9.     try:
  10.         return [f for f in os.listdir(directory) if f.endswith('.lnk') or f.endswith('.exe')]
  11.     except Exception as e:
  12.         messagebox.showerror("Error", f"Failed to list files in directory: {e}")
  13.         return []
  14.  
  15. def open_file(filepath):
  16.     """Open the selected .lnk or .exe file."""
  17.     try:
  18.         # Using os.startfile to open the file, which works for both .lnk and .exe
  19.         os.startfile(filepath)
  20.     except Exception as e:
  21.         messagebox.showerror("Error", f"Failed to open file: {e}")
  22.  
  23. def on_file_select(event):
  24.     """Event handler for file selection."""
  25.     selected_index = file_listbox.curselection()
  26.     if selected_index:
  27.         selected_file = file_listbox.get(selected_index)
  28.         open_file(os.path.join(folder_path, selected_file))
  29.  
  30. # Specify the folder containing .lnk and .exe files
  31. folder_path = 'C:/Users/Najeeb/Desktop/FTP'
  32.  
  33. # List all .lnk and .exe files in the folder
  34. files = list_files(folder_path)
  35.  
  36. # Create the main window with a theme
  37. root = ThemedTk(theme="arc")
  38. root.title("Select a .lnk or .exe File to Open")
  39.  
  40. # Create a frame for better layout control
  41. frame = ttk.Frame(root, padding="10")
  42. frame.pack(fill=tk.BOTH, expand=True)
  43.  
  44. # Create a Listbox to display the .lnk and .exe files
  45. file_listbox = tk.Listbox(frame, selectmode=tk.SINGLE, height=15)
  46. for file in files:
  47.     file_listbox.insert(tk.END, file)
  48. file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
  49.  
  50. # Create a scrollbar for the Listbox
  51. scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=file_listbox.yview)
  52. scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
  53. file_listbox.config(yscrollcommand=scrollbar.set)
  54.  
  55. # Bind the selection event to the handler
  56. file_listbox.bind('<<ListboxSelect>>', on_file_select)
  57.  
  58. # Start the GUI event loop
  59. root.mainloop()
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement