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 = file1_entry.get()
file2 = file2_entry.get()
if file1 and file2:
command = f"your_command {file1} {file2}"
subprocess.run(command, shell=True)
else:
print("Please select both files.")
# Create the main window
window = tk.Tk()
window.title("File Selector")
# Create the 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 the 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 the run button
run_button = tk.Button(window, text="Run", command=run_command)
run_button.pack()
# Start the main event loop
window.mainloop()
This GUI provides the following functionality:
-
It creates a main window with the title "File Selector".
-
It creates two file selectors, one for "File 1" and another for "File 2". Each file selector consists of a label, an entry field to display the selected file path, and a button to open the file dialog.
-
When the "Select File 1" or "Select File 2" button is clicked, it opens a file dialog using filedialog.askopenfilename() to allow the user to select a file. The selected file path is then inserted into the corresponding entry field.
-
It creates a "Run" button at the bottom of the window.
-
When the "Run" button is clicked, it retrieves the file paths from the entry fields and checks if both files are selected.
-
If both files are selected, it constructs a command string using the selected file paths. You need to replace "your_command" with the actual command you want to run on the files.
-
The command is executed using subprocess.run() with shell=True to allow the command to be interpreted by the shell.
-
If either of the files is not selected, it prints a message indicating that both files need to be selected.
-
Finally, it starts the main event loop using window.mainloop() to display the GUI and handle user interactions.
Note: Make sure to replace "your_command" with the actual command you want to run on the selected files.