refactor: Improve file dialog handling for cross-platform compatibility
This commit is contained in:
parent
f010ee57cf
commit
c4e107b40e
@ -47,11 +47,11 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
|
|||||||
from tkinter import filedialog
|
from tkinter import filedialog
|
||||||
root = tk.Tk()
|
root = tk.Tk()
|
||||||
root.withdraw() # Hide the main window
|
root.withdraw() # Hide the main window
|
||||||
root.attributes('-topmost', True) # Bring dialog to front
|
root.wm_attributes('-topmost', 1) # Bring dialog to front
|
||||||
path = filedialog.askopenfilename(title=title, filetypes=file_types)
|
path = filedialog.askopenfilename(title=title, filetypes=file_types)
|
||||||
|
root.destroy() # Explicitly destroy the tkinter instance
|
||||||
return path if path else None
|
return path if path else None
|
||||||
elif platform.system() == "Linux":
|
elif platform.system() == "Linux":
|
||||||
# Use zenity for Linux systems
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
['zenity', '--file-selection', '--title', title],
|
['zenity', '--file-selection', '--title', title],
|
||||||
@ -60,6 +60,7 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
|
|||||||
)
|
)
|
||||||
return result.stdout.strip() if result.returncode == 0 else None
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
st.error("Zenity is not installed. Please install it for file dialog support.")
|
||||||
return None
|
return None
|
||||||
elif platform.system() == "Darwin": # macOS
|
elif platform.system() == "Darwin": # macOS
|
||||||
try:
|
try:
|
||||||
@ -70,6 +71,7 @@ def get_file_path(title="Select File", file_types=[("Text Files", "*.txt")]):
|
|||||||
)
|
)
|
||||||
return result.stdout.strip() if result.returncode == 0 else None
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
st.error("AppleScript is not available for file dialog support.")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
st.error(f"Error opening file dialog: {str(e)}")
|
st.error(f"Error opening file dialog: {str(e)}")
|
||||||
@ -84,11 +86,11 @@ def get_directory_path(title="Select Directory"):
|
|||||||
from tkinter import filedialog
|
from tkinter import filedialog
|
||||||
root = tk.Tk()
|
root = tk.Tk()
|
||||||
root.withdraw() # Hide the main window
|
root.withdraw() # Hide the main window
|
||||||
root.attributes('-topmost', True) # Bring dialog to front
|
root.wm_attributes('-topmost', 1) # Bring dialog to front
|
||||||
path = filedialog.askdirectory(title=title)
|
path = filedialog.askdirectory(title=title)
|
||||||
|
root.destroy() # Explicitly destroy the tkinter instance
|
||||||
return path if path else None
|
return path if path else None
|
||||||
elif platform.system() == "Linux":
|
elif platform.system() == "Linux":
|
||||||
# Use zenity for Linux systems
|
|
||||||
try:
|
try:
|
||||||
result = subprocess.run(
|
result = subprocess.run(
|
||||||
['zenity', '--file-selection', '--directory', '--title', title],
|
['zenity', '--file-selection', '--directory', '--title', title],
|
||||||
@ -97,6 +99,7 @@ def get_directory_path(title="Select Directory"):
|
|||||||
)
|
)
|
||||||
return result.stdout.strip() if result.returncode == 0 else None
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
st.error("Zenity is not installed. Please install it for file dialog support.")
|
||||||
return None
|
return None
|
||||||
elif platform.system() == "Darwin": # macOS
|
elif platform.system() == "Darwin": # macOS
|
||||||
try:
|
try:
|
||||||
@ -107,6 +110,7 @@ def get_directory_path(title="Select Directory"):
|
|||||||
)
|
)
|
||||||
return result.stdout.strip() if result.returncode == 0 else None
|
return result.stdout.strip() if result.returncode == 0 else None
|
||||||
except FileNotFoundError:
|
except FileNotFoundError:
|
||||||
|
st.error("AppleScript is not available for file dialog support.")
|
||||||
return None
|
return None
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
st.error(f"Error opening file dialog: {str(e)}")
|
st.error(f"Error opening file dialog: {str(e)}")
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user