Here's a basic Python GUI using the tkinter library that allows you to select two files and run a command on both of them when the "Run" button is clicked:
import tkinter as tk
from tkinter import filedialog
import subprocess
def select_file1():
file_path = filedialog.askopenfilename()
file1_entry.delete(0, tk.END)
file1_entry.insert(0, file_path)
def select_file2():
file_path = filedialog.askopenfilename()
file2_entry.delete(0, tk.END)
file2_entry.insert(0, file_path)
def run_command():
file1_path = file1_entry.get()
file2_path = file2_entry.get()
if file1_path and file2_path:
command = f"your_command {file1_path} {file2_path}"
subprocess.run(command, shell=True)
else:
print("Please select both files.")
# Create the main window
window = tk.Tk()
window.title("File Selector")
# Create file selector for file 1
file1_label = tk.Label(window, text="File 1:")
file1_label.pack()
file1_entry = tk.Entry(window, width=50)
file1_entry.pack()
file1_button = tk.Button(window, text="Select File 1", command=select_file1)
file1_button.pack()
# Create file selector for file 2
file2_label = tk.Label(window, text="File 2:")
file2_label.pack()
file2_entry = tk.Entry(window, width=50)
file2_entry.pack()
file2_button = tk.Button(window, text="Select File 2", command=select_file2)
file2_button.pack()
# Create run button
run_button = tk.Button(window, text="Run", command=run_command)
run_button.pack()
# Start the main event loop
window.mainloop()
This GUI does the following:
-
We import the necessary modules: tkinter for creating the GUI, filedialog for file selection, and subprocess for running commands.
-
We define three functions:
select_file1(): Opens a file dialog to select the first file and updates the corresponding entry field with the selected file path.
select_file2(): Opens a file dialog to select the second file and updates the corresponding entry field with the selected file path.
run_command(): Retrieves the file paths from the entry fields and runs a command on both files using subprocess.run(). You need to replace "your_command" with the actual command you want to run.
-
We create the main window using tk.Tk() and set its title.
-
We create the file selector for the first file:
- A label to display "File 1:"
- An entry field to show the selected file path
- A button to open the file dialog for selecting the first file
-
We create the file selector for the second file, similar to the first file selector.
-
We create a "Run" button that triggers the run_command() function when clicked.
-
Finally, we start the main event loop using window.mainloop() to display the GUI and handle user interactions.
When you run this script, a window will appear with two file selectors and a "Run" button. You can click the "Select File 1" and "Select File 2" buttons to choose the respective files, and then click the "Run" button to execute the specified command on both files.
Note: Make sure to replace "your_command" with the actual command you want to run on the selected files.