123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import os
- import subprocess
- from tkinter import Tk, Listbox, Button, END, Label
- class PythonFileExecutor:
- def __init__(self, root_directory):
- self.root_directory = root_directory
- self.python_files = self.find_python_files()
- self.window = Tk()
- self.window.title("Python File Executor")
- self.create_widgets()
- def find_python_files(self):
- """ Find all Python files in the given directory. """
- python_files = []
- for root, dirs, files in os.walk(self.root_directory):
- for file in files:
- if file.endswith('.py'):
- python_files.append(os.path.join(root, file))
- return python_files
- def create_widgets(self):
- """ Create UI elements for the application. """
- Label(self.window, text="Select a Python file to execute:").pack()
- self.listbox = Listbox(self.window, width=50, height=15)
- for file in self.python_files:
- self.listbox.insert(END, file)
- self.listbox.pack()
- Button(self.window, text="Execute", command=self.execute_selected_file).pack()
- def execute_selected_file(self):
- """ Execute the selected Python file. """
- selection = self.listbox.curselection()
- if selection:
- file_to_execute = self.python_files[selection[0]]
- try:
- subprocess.run(['python', file_to_execute], check=True)
- except subprocess.CalledProcessError as e:
- print(f"Error executing {file_to_execute}: {str(e)}")
- else:
- print("Please select a file to execute.")
- def run(self):
- """ Run the GUI event loop. """
- self.window.mainloop()
- if __name__ == '__main__':
- path = "D:\\hiddz\\SAR\\test_data\\xie"
- executor = PythonFileExecutor(path)
- executor.run()
|