feature_qt.py 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. import os
  2. import subprocess
  3. from tkinter import Tk, Listbox, Button, END, Label
  4. class PythonFileExecutor:
  5. def __init__(self, root_directory):
  6. self.root_directory = root_directory
  7. self.python_files = self.find_python_files()
  8. self.window = Tk()
  9. self.window.title("Python File Executor")
  10. self.create_widgets()
  11. def find_python_files(self):
  12. """ Find all Python files in the given directory. """
  13. python_files = []
  14. for root, dirs, files in os.walk(self.root_directory):
  15. for file in files:
  16. if file.endswith('.py'):
  17. python_files.append(os.path.join(root, file))
  18. return python_files
  19. def create_widgets(self):
  20. """ Create UI elements for the application. """
  21. Label(self.window, text="Select a Python file to execute:").pack()
  22. self.listbox = Listbox(self.window, width=50, height=15)
  23. for file in self.python_files:
  24. self.listbox.insert(END, file)
  25. self.listbox.pack()
  26. Button(self.window, text="Execute", command=self.execute_selected_file).pack()
  27. def execute_selected_file(self):
  28. """ Execute the selected Python file. """
  29. selection = self.listbox.curselection()
  30. if selection:
  31. file_to_execute = self.python_files[selection[0]]
  32. try:
  33. subprocess.run(['python', file_to_execute], check=True)
  34. except subprocess.CalledProcessError as e:
  35. print(f"Error executing {file_to_execute}: {str(e)}")
  36. else:
  37. print("Please select a file to execute.")
  38. def run(self):
  39. """ Run the GUI event loop. """
  40. self.window.mainloop()
  41. if __name__ == '__main__':
  42. path = "D:\\hiddz\\SAR\\test_data\\xie"
  43. executor = PythonFileExecutor(path)
  44. executor.run()