refactor: Improve tkinter file dialog thread safety

This commit is contained in:
Bobby Abellana (aider) 2025-02-11 16:27:49 -08:00
parent 302c977027
commit dfe99a9733

View File

@ -210,22 +210,31 @@ with col1:
else: # Windows or Linux else: # Windows or Linux
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
root = tk.Tk() import threading
root.withdraw() import queue
root.wm_attributes('-topmost', 1)
root.after(10) # Add a small delay # Create a queue to pass the selected path between threads
# Create and run the main loop in a way that doesn't block path_queue = queue.Queue()
path_holder = []
def select_dir(): def create_file_dialog():
root = tk.Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
path = filedialog.askdirectory(title="Select Source Directory") path = filedialog.askdirectory(title="Select Source Directory")
path_holder.append(path) path_queue.put(path)
root.quit() root.destroy()
root.after(20, select_dir)
root.mainloop() # Create and start the dialog in a new thread
if path_holder and path_holder[0]: dialog_thread = threading.Thread(target=create_file_dialog)
st.session_state['source_dir_selected'] = path_holder[0] dialog_thread.start()
dialog_thread.join() # Wait for the thread to complete
# Get the selected path from the queue
selected_path = path_queue.get()
if selected_path:
st.session_state['source_dir_selected'] = selected_path
st.rerun() st.rerun()
root.destroy()
except Exception as e: except Exception as e:
st.error(f"Error selecting directory: {str(e)}") st.error(f"Error selecting directory: {str(e)}")
@ -267,22 +276,31 @@ with col1:
else: # Windows or Linux else: # Windows or Linux
import tkinter as tk import tkinter as tk
from tkinter import filedialog from tkinter import filedialog
root = tk.Tk() import threading
root.withdraw() import queue
root.wm_attributes('-topmost', 1)
root.after(10) # Add a small delay # Create a queue to pass the selected path between threads
# Create and run the main loop in a way that doesn't block path_queue = queue.Queue()
path_holder = []
def select_dir(): def create_file_dialog():
root = tk.Tk()
root.withdraw()
root.wm_attributes('-topmost', 1)
path = filedialog.askdirectory(title="Select Destination Directory") path = filedialog.askdirectory(title="Select Destination Directory")
path_holder.append(path) path_queue.put(path)
root.quit() root.destroy()
root.after(20, select_dir)
root.mainloop() # Create and start the dialog in a new thread
if path_holder and path_holder[0]: dialog_thread = threading.Thread(target=create_file_dialog)
st.session_state['dest_dir_selected'] = path_holder[0] dialog_thread.start()
dialog_thread.join() # Wait for the thread to complete
# Get the selected path from the queue
selected_path = path_queue.get()
if selected_path:
st.session_state['dest_dir_selected'] = selected_path
st.rerun() st.rerun()
root.destroy()
except Exception as e: except Exception as e:
st.error(f"Error selecting directory: {str(e)}") st.error(f"Error selecting directory: {str(e)}")