Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import os
- import subprocess
- import tkinter as tk
- from tkinter import messagebox, ttk
- from ttkthemes import ThemedTk
- def list_files(directory):
- """List all .lnk and .exe files in the specified directory."""
- try:
- return [f for f in os.listdir(directory) if f.endswith('.lnk') or f.endswith('.exe')]
- except Exception as e:
- messagebox.showerror("Error", f"Failed to list files in directory: {e}")
- return []
- def open_file(filepath):
- """Open the selected .lnk or .exe file."""
- try:
- # Using os.startfile to open the file, which works for both .lnk and .exe
- os.startfile(filepath)
- except Exception as e:
- messagebox.showerror("Error", f"Failed to open file: {e}")
- def on_file_select(event):
- """Event handler for file selection."""
- selected_index = file_listbox.curselection()
- if selected_index:
- selected_file = file_listbox.get(selected_index)
- open_file(os.path.join(folder_path, selected_file))
- # Specify the folder containing .lnk and .exe files
- folder_path = 'C:/Users/Najeeb/Desktop/FTP'
- # List all .lnk and .exe files in the folder
- files = list_files(folder_path)
- # Create the main window with a theme
- root = ThemedTk(theme="arc")
- root.title("Select a .lnk or .exe File to Open")
- # Create a frame for better layout control
- frame = ttk.Frame(root, padding="10")
- frame.pack(fill=tk.BOTH, expand=True)
- # Create a Listbox to display the .lnk and .exe files
- file_listbox = tk.Listbox(frame, selectmode=tk.SINGLE, height=15)
- for file in files:
- file_listbox.insert(tk.END, file)
- file_listbox.pack(side=tk.LEFT, fill=tk.BOTH, expand=True, padx=(0, 5))
- # Create a scrollbar for the Listbox
- scrollbar = ttk.Scrollbar(frame, orient=tk.VERTICAL, command=file_listbox.yview)
- scrollbar.pack(side=tk.RIGHT, fill=tk.Y)
- file_listbox.config(yscrollcommand=scrollbar.set)
- # Bind the selection event to the handler
- file_listbox.bind('<<ListboxSelect>>', on_file_select)
- # Start the GUI event loop
- root.mainloop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement