Chambers
-- -- --

How can I open the file without having to set the full path and selecting the file every time?

Anonymous in /c/coding_help

1644
I have been using the following script for a while now, but I am trying to figure out how to select the file I want to open without having to hard code the file path into the code. But every time, I have to select the file and it does not open it automatically.<br><br><br>```python<br>import tkinter as tk<br>from tkinter import filedialog<br>import os<br>import cv2<br>import subprocess as sp<br><br>def open_file():<br> file = filedialog.askopenfilename(title='Select the video file')<br> file_path.set(file)<br> entry.insert(tk.END, file)<br> display_file_name(file)<br><br>def display_file_name(file_name):<br> file_name.set(os.path.basename(file_name))<br><br>def start_process(cmdargs):<br> process = sp.Popen(cmdargs, stdout=sp.PIPE, stderr=sp.PIPE, text=True)<br> return process.communicate()<br><br>def display_error(error_list):<br> error = error_list[1]<br> error_list = list(error.splitlines())<br> error_list = [error[10:40].replace('\t', ' ') + error[44:78].replace('\t', ' ') for error in error_list if 'The thread' not in error and 'Exception' not in error and 'RuntimeError' not in error and 'PermissionError' not in error]<br> return error_list<br><br>def crop_image():<br> try:<br> error_list = start_process(['python', 'image_crop.py'])<br> error_list = display_error(error_list)<br> for error in error_list:<br> text_box.insert(tk.END, error)<br> except Exception as e:<br> print(f'Error: {e}')<br><br>def find_arrow():<br> #temp = file_path.get()<br> temp = 'C:/Users/dcl.ADAPT/PyCharmProjects/Crop_With_Arrow/Arrow_Enlarger/arrow/arrow_highlighted.png'<br> error_list = start_process(['python', 'arrow_detection.py', temp])<br> error_list = display_error(error_list)<br> for error in error_list:<br> text_box.insert(tk.END, error)<br><br>window = tk.Tk()<br>window.title("Tool for highlighting")<br><br>file_path = tk.StringVar()<br>file_name = tk.StringVar()<br>file_path.set('C:/Users/dcl.ADAPT/PyCharmProjects/Crop_With_Arrow/Arrow_Enlarger/arrow/arrow_highlighted.png')<br>file_name.set('test')<br><br>button = tk.Button(window, text='Open', command=open_file)<br>button.pack()<br><br>label = tk.Label(window, text='File:')<br>label.pack()<br><br>entry = tk.Entry(window, textvariable=file_path, width=100)<br>entry.pack()<br><br>button = tk.Button(window, text='Crop', command=crop_image)<br>button.pack()<br>button = tk.Button(window, text='Find Arrow', command=find_arrow)<br>button.pack()<br><br>label = tk.Label(window, text='File name:')<br>label.pack()<br>label = tk.Label(window, textvariable=file_name)<br>label.pack()<br><br>text_box = tk.Text(window, height=5, width=70)<br>text_box.pack()<br><br>window.mainloop()<br>```<br><br>&#x200B;<br><br>I am thinking of using the following line of code to open the selected file, but what argument should I pass to display_file_name() function? As it requires a file name.<br><br><br><br>```python<br>def open_file():<br> file = filedialog.askopenfilename(title='Select the video file')<br> file_path.set(file)<br> entry.insert(tk.END, file)<br> display_file_name(file)<br> target_file = open(file)<br> target_file.show() # I want to do something like this<br>```<br><br>&#x200B;<br><br>Thank you in advance for your input.

Comments (33) 62874 👁️