Skip to content

Commit 3424286

Browse files
committed
trying to add File -> Open
1 parent fdd9f0d commit 3424286

File tree

2 files changed

+56
-26
lines changed

2 files changed

+56
-26
lines changed

pyptv/file_editor_demo.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from traits.api import HasTraits, File
2+
from traitsui.api import View, Item, FileEditor
3+
4+
class FilteredFileBrowserExample(HasTraits):
5+
"""
6+
An example showing how to filter for specific file types.
7+
"""
8+
file_path = File()
9+
10+
view = View(
11+
Item('file_path',
12+
label="Select a YAML File",
13+
editor=FileEditor(filter=['*.yaml','*.yml']),
14+
),
15+
title="YAML File Browser",
16+
buttons=['OK', 'Cancel'],
17+
resizable=True
18+
)
19+
20+
if __name__ == '__main__':
21+
filtered_browser_instance = FilteredFileBrowserExample()
22+
filtered_browser_instance.configure_traits()
23+
if filtered_browser_instance.file_path:
24+
print(f"\nYou selected the Python file: {filtered_browser_instance.file_path}")
25+
else:
26+
print("\nNo file was selected.")

pyptv/pyptv_gui.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,21 @@
4141
"""
4242
ETSConfig.toolkit = "qt"
4343

44+
class FilteredFileBrowserExample(HasTraits):
45+
"""
46+
An example showing how to filter for specific file types.
47+
"""
48+
file_path = File()
49+
50+
view = View(
51+
Item('file_path',
52+
label="Select a YAML File",
53+
editor=FileEditor(filter=['*.yaml','*.yml']),
54+
),
55+
title="YAML File Browser",
56+
buttons=['OK', 'Cancel'],
57+
resizable=True
58+
)
4459

4560
class Clicker(ImageInspectorTool):
4661
"""
@@ -134,7 +149,7 @@ def attach_tools(self):
134149

135150
pan = PanTool(self._plot, drag_button="middle")
136151
zoom_tool = ZoomTool(self._plot, tool_mode="box", always_on=False)
137-
zoom_tool.max_zoom_out_factor = 1.0 # Disable "bird view" zoom out
152+
# zoom_tool.max_zoom_out_factor = 1.0 # Disable "bird view" zoom out
138153
self._img_plot.overlays.append(zoom_tool)
139154
self._img_plot.tools.append(pan)
140155
# print(self._img_plot.tools)
@@ -437,31 +452,20 @@ def new_action(self, info):
437452
print("not implemented")
438453

439454
def open_action(self, info):
440-
from tkinter import filedialog
441-
import tkinter as tk
442-
443-
root = tk.Tk()
444-
root.withdraw()
445-
file_path = filedialog.askopenfilename(
446-
title="Select YAML parameter file",
447-
filetypes=[("YAML files", "*.yaml *.yml")]
448-
)
449-
if file_path:
450-
try:
451-
yaml_file = Path(file_path)
452-
# Re-initialize MainGUI with the new YAML file
453-
# Close the current GUI and open a new one
454-
print(f"Opening new experiment from {yaml_file}")
455-
# Save current working directory
456-
cwd = Path.cwd()
457-
os.chdir(yaml_file.parent)
458-
new_gui = MainGUI(yaml_file)
459-
new_gui.configure_traits()
460-
os.chdir(cwd)
461-
except Exception as e:
462-
print(f"Failed to open YAML file: {e}")
463-
else:
464-
print("No YAML file selected.")
455+
456+
filtered_browser_instance = FilteredFileBrowserExample()
457+
filtered_browser_instance.configure_traits()
458+
if filtered_browser_instance.file_path:
459+
print(f"\nYou selected the YAML file: {filtered_browser_instance.file_path}")
460+
yaml_path = Path(filtered_browser_instance.file_path)
461+
if yaml_path.is_file() and yaml_path.suffix in {".yaml", ".yml"}:
462+
print(f"Initializing MainGUI with selected YAML: {yaml_path}")
463+
os.chdir(yaml_path.parent) # Change to the directory of the YAML file
464+
main_gui = MainGUI(yaml_path)
465+
main_gui.configure_traits()
466+
else:
467+
print("\nNo file was selected.")
468+
465469

466470
def exit_action(self, info):
467471
print("not implemented")

0 commit comments

Comments
 (0)