Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions pds4_tools/utils/compat.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,22 @@
except ImportError:
from ..extern import argparse


# Tkinter compat (Python 3.6+)
def tk_trace_add(variable, mode, callback):
try:
return variable.trace_add(mode, callback)
except AttributeError:
return variable.trace(mode[0], callback)


def tk_trace_remove(variable, mode, callback_id):
try:
return variable.trace_remove(mode, callback_id)
except AttributeError:
return variable.trace_vdelete(mode[0], callback_id)


# ElementTree compat (Python 2.7+ and 3.3+)
ET_Element = ET.Element if isinstance(ET.Element, six.class_types) else ET._Element
ET_Tree_iter = ET.ElementTree.iter if hasattr(ET.ElementTree, 'iter') else ET.ElementTree.getiterator
Expand Down
15 changes: 8 additions & 7 deletions pds4_tools/viewer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
import subprocess

from . import cache
from ..utils.compat import argparse
from ..utils.compat import argparse, tk_trace_add, tk_trace_remove
from ..utils.helpers import is_array_like
from ..utils.logging import logger_init

Expand Down Expand Up @@ -411,18 +411,19 @@ def _process_callbacks(self, name=None, with_removal=True):
self._callbacks.pop(name, None)

# Convenience wrapper around creating a trace for TK's variables. A trace uses a TK variable, and
# automatically runs the callback when a mode action is done. Modes available are 'w', called when the
# TK variable is written to, 'r' when a TK variable is read from and 'u' for when the variable is deleted
# automatically runs the callback when a mode action is done. Modes available are 'write', called
# when the TK variable is written to, 'read' when a TK variable is read from and 'unset' for when
# the variable is deleted
def _add_trace(self, variable, mode, callback, default=None):

if default is not None:
variable.set(default)

trace_id = variable.trace(mode, callback)
trace_id = tk_trace_add(variable, mode, callback)

# Create a callback to delete the trace, otherwise it will stay bound and prevent python
# from clearing window memory on close
self._add_callback('close', variable.trace_vdelete, 'w', trace_id)
self._add_callback('close', tk_trace_remove, variable, mode, trace_id)

return trace_id

Expand Down Expand Up @@ -787,11 +788,11 @@ def __init__(self, viewer):

# Stores search string in search box
self._search_text = StringVar()
self._add_trace(self._search_text, 'w', self._do_search)
self._add_trace(self._search_text, 'write', self._do_search)

# Stores whether match case box is selected
self._match_case = BooleanVar()
self._add_trace(self._match_case, 'w', self._do_search, default=False)
self._add_trace(self._match_case, 'write', self._do_search, default=False)

# Stores a list of 3-valued tuples, each containing the line number, start position
# and stop position of each result that matches the search string; and stores the index
Expand Down
8 changes: 4 additions & 4 deletions pds4_tools/viewer/image_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ def __init__(self, viewer):
var = option['type']
self._menu_options[option['name']] = var

self._add_trace(var, 'w', option['trace'], option['default'])
self._add_trace(var, 'write', option['trace'], option['default'])

@property
def settings(self):
Expand Down Expand Up @@ -2461,7 +2461,7 @@ def __init__(self, viewer, image_structure_window):

self._selected_axis = IntVar()
self._selected_axis.set(self._structure_window.settings['selected_axis'])
self._add_trace(self._selected_axis, 'w',
self._add_trace(self._selected_axis, 'write',
lambda *args: self._structure_window.select_slice(axis=self._selected_axis.get()))

self._sliders = []
Expand Down Expand Up @@ -2514,7 +2514,7 @@ def _create_axis_row(self, slider_index, axis_name, axis_sequence, axis_slice, m

slider_var = IntVar()
slider_var.set(axis_slice)
self._add_trace(slider_var, 'w', lambda *args: self._slider_moved(slider_index, axis_sequence))
self._add_trace(slider_var, 'write', lambda *args: self._slider_moved(slider_index, axis_sequence))

slider_row_box = Frame(self._sliders_box, bg=self.get_bg('gray'))
slider_row_box.pack(pady=(20, 10), expand=1, fill='x')
Expand Down Expand Up @@ -2593,7 +2593,7 @@ def __init__(self, viewer, image_structure_window, image):
var = option['type']
self._menu_options[option['name']] = var

self._add_trace(var, 'w', option['trace'], option['default'])
self._add_trace(var, 'write', option['trace'], option['default'])

# Add the menu
self._add_menus()
Expand Down
4 changes: 2 additions & 2 deletions pds4_tools/viewer/label_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ def __init__(self, viewer, full_label, structure_label=None, initial_display='fu
# Stores what display type is currently selected
display_type = StringVar()
self._menu_options['display_type'] = display_type
self._add_trace(display_type, 'w', self._update_label, default=initial_display)
self._add_trace(display_type, 'write', self._update_label, default=initial_display)

# Stores whether label is being pretty printed or shown as in the file
pretty_print = BooleanVar()
self._menu_options['pretty_print'] = pretty_print
self._add_trace(pretty_print, 'w', self._update_label, default=True)
self._add_trace(pretty_print, 'write', self._update_label, default=True)

# Draw the main window content
self._set_heading('Label')
Expand Down
8 changes: 5 additions & 3 deletions pds4_tools/viewer/plot_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def __init__(self, viewer):
var = option['type']
self._menu_options[option['name']] = var

self._add_trace(var, 'w', option['trace'], option['default'])
self._add_trace(var, 'write', option['trace'], option['default'])

@property
def settings(self):
Expand Down Expand Up @@ -2469,7 +2469,8 @@ def _add_axes_tab(self):

current_axis_limits = self._structure_window.menu_option('axis_limits')
axis_limits = self._axes_options['axis_limits'] = StringVar()
self._add_trace(axis_limits, 'w', self._update_limits_setting, default=current_axis_limits.capitalize())
self._add_trace(axis_limits, 'write',
self._update_limits_setting, default=current_axis_limits.capitalize())

menu = OptionMenu(option_menus_box, axis_limits, *('Intelligent', 'Tight', 'Auto', 'Manual'))
menu.config(width=15, **option_menu_params)
Expand All @@ -2480,7 +2481,8 @@ def _add_axes_tab(self):
manual_limits_box.pack(side='top', anchor='nw', pady=10)

manual_limits = BooleanVar()
self._add_trace(manual_limits, 'w', self._update_limits_setting, default=(current_axis_limits == 'manual'))
self._add_trace(manual_limits, 'write',
self._update_limits_setting, default=(current_axis_limits == 'manual'))

b = Checkbutton(manual_limits_box, text='Manual Limits', variable=axis_limits,
onvalue='Manual', offvalue='Intelligent', **text_params)
Expand Down
9 changes: 6 additions & 3 deletions pds4_tools/viewer/summary_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -546,13 +546,16 @@ def _add_menus(self, quiet, lazy_load, show_headers):

# Initialize menu options
self._menu_options['quiet'] = BooleanVar()
self._add_trace(self._menu_options['quiet'], 'w', self._update_quiet, default=quiet)
self._add_trace(self._menu_options['quiet'], 'write',
self._update_quiet, default=quiet)

self._menu_options['lazy_load'] = BooleanVar()
self._add_trace(self._menu_options['lazy_load'], 'w', self._update_lazy_load, default=lazy_load)
self._add_trace(self._menu_options['lazy_load'], 'write',
self._update_lazy_load, default=lazy_load)

self._menu_options['show_headers'] = BooleanVar()
self._add_trace(self._menu_options['show_headers'], 'w', self._update_show_headers, default=show_headers)
self._add_trace(self._menu_options['show_headers'], 'write',
self._update_show_headers, default=show_headers)

# Add a File menu
file_menu = self._add_menu('File', in_menu='main')
Expand Down
2 changes: 1 addition & 1 deletion pds4_tools/viewer/table_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def __init__(self, viewer):
var = option['type']
self._menu_options[option['name']] = var

self._add_trace(var, 'w', option['trace'], option['default'])
self._add_trace(var, 'write', option['trace'], option['default'])

# These variables are used to store widgets, and info about them, used for displaying tabular data
self._data_boxes = []
Expand Down