refactor: Improve tkinter file dialog thread safety
This commit is contained in:
parent
302c977027
commit
dfe99a9733
@ -210,22 +210,31 @@ with col1:
|
||||
else: # Windows or Linux
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
root.wm_attributes('-topmost', 1)
|
||||
root.after(10) # Add a small delay
|
||||
# Create and run the main loop in a way that doesn't block
|
||||
path_holder = []
|
||||
def select_dir():
|
||||
import threading
|
||||
import queue
|
||||
|
||||
# Create a queue to pass the selected path between threads
|
||||
path_queue = queue.Queue()
|
||||
|
||||
def create_file_dialog():
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
root.wm_attributes('-topmost', 1)
|
||||
path = filedialog.askdirectory(title="Select Source Directory")
|
||||
path_holder.append(path)
|
||||
root.quit()
|
||||
root.after(20, select_dir)
|
||||
root.mainloop()
|
||||
if path_holder and path_holder[0]:
|
||||
st.session_state['source_dir_selected'] = path_holder[0]
|
||||
path_queue.put(path)
|
||||
root.destroy()
|
||||
|
||||
# Create and start the dialog in a new thread
|
||||
dialog_thread = threading.Thread(target=create_file_dialog)
|
||||
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()
|
||||
root.destroy()
|
||||
except Exception as e:
|
||||
st.error(f"Error selecting directory: {str(e)}")
|
||||
|
||||
@ -267,22 +276,31 @@ with col1:
|
||||
else: # Windows or Linux
|
||||
import tkinter as tk
|
||||
from tkinter import filedialog
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
root.wm_attributes('-topmost', 1)
|
||||
root.after(10) # Add a small delay
|
||||
# Create and run the main loop in a way that doesn't block
|
||||
path_holder = []
|
||||
def select_dir():
|
||||
import threading
|
||||
import queue
|
||||
|
||||
# Create a queue to pass the selected path between threads
|
||||
path_queue = queue.Queue()
|
||||
|
||||
def create_file_dialog():
|
||||
root = tk.Tk()
|
||||
root.withdraw()
|
||||
root.wm_attributes('-topmost', 1)
|
||||
path = filedialog.askdirectory(title="Select Destination Directory")
|
||||
path_holder.append(path)
|
||||
root.quit()
|
||||
root.after(20, select_dir)
|
||||
root.mainloop()
|
||||
if path_holder and path_holder[0]:
|
||||
st.session_state['dest_dir_selected'] = path_holder[0]
|
||||
path_queue.put(path)
|
||||
root.destroy()
|
||||
|
||||
# Create and start the dialog in a new thread
|
||||
dialog_thread = threading.Thread(target=create_file_dialog)
|
||||
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()
|
||||
root.destroy()
|
||||
except Exception as e:
|
||||
st.error(f"Error selecting directory: {str(e)}")
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user