# spacr.gui_elements

## Module Contents

spacr.gui_elements.fig = None

spacr.gui_elements.restart_gui_app(root)

  Restarts the GUI application by destroying the current instance and launching a fresh one.

spacr.gui_elements.create_menu_bar(root)

spacr.gui_elements.set_element_size()

spacr.gui_elements.set_dark_style(style, parent_frame=None, containers=None, widgets=None, font_family='OpenSans', font_size=12, bg_color='black', fg_color='white', active_color='blue', inactive_color='dark_gray')

class spacr.gui_elements.spacrFont(font_name, font_style, font_size=12)
  font_name
  font_style
  font_size = 12
  font_path
  get_font_path(font_name, font_style)

    Returns the font path based on the font name and style.

    Parameters: - font_name: str, the name of the font. - font_style: str, the style of the font.

    Returns: - str, the path to the font file.

  load_font()

    Loads the font into Tkinter.

  get_font(size=None)

    Returns the font in the specified size.

    Parameters: - size: int, the size of the font (optional).

    Returns: - tkFont.Font object.

class spacr.gui_elements.spacrContainer(parent, orient=tk.VERTICAL, bg=None, *args, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  orient = 'vertical'
  bg = 'lightgrey'
  sash_thickness = 10
  panes = []
  sashes = []
  add(widget, stretch='always')
  create_sash()
  reposition_panes()
  on_configure(event)
  on_enter_sash(event)
  on_leave_sash(event)
  start_resize(event)
  perform_resize(event)

class spacr.gui_elements.spacrEntry(parent, textvariable=None, outline=False, width=None, *args, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  bg_color = 'dark_gray'
  active_color = 'blue'
  fg_color = 'white'
  outline = False
  font_family = 'OpenSans'
  font_size = 12
  font_loader
  canvas_height = 40
  canvas
  draw_rounded_rectangle(color)
  on_focus_in(event)
  on_focus_out(event)

class spacr.gui_elements.spacrCheck(parent, text='', variable=None, *args, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  bg_color = 'black'
  active_color = 'blue'
  fg_color = 'white'
  inactive_color = 'dark_gray'
  variable = None
  canvas_width = 20
  canvas_height = 20
  canvas
  draw_rounded_square(color)
  update_check(*args)
  toggle_variable(event)

class spacr.gui_elements.spacrCombo(parent, textvariable=None, values=None, width=None, *args, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  bg_color = 'black'
  active_color = 'blue'
  fg_color = 'white'
  inactive_color = 'dark_gray'
  font_family = 'OpenSans'
  font_size = 12
  font_loader
  values = []
  canvas_width = None
  canvas_height = 40
  canvas
  var
  selected_value
  dropdown_menu = None
  draw_rounded_rectangle(color)
  on_click(event)
  open_dropdown()
  close_dropdown()
  on_select(value)
  set(value)

class spacr.gui_elements.spacrDropdownMenu(parent, variable, options, command=None, font=None, size=50, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  variable
  options
  command = None
  text = 'Settings'
  size = 50
  font_size = 12
  font_loader
  button_width = 150
  canvas_width = 154
  canvas_height = 54
  canvas
  inactive_color = 'dark_gray'
  active_color = 'blue'
  fg_color = 'white'
  bg_color = 'black'
  button_bg
  button_text
  menu
  create_rounded_rectangle(x1, y1, x2, y2, radius=20, **kwargs)
  on_enter(event=None)
  on_leave(event=None)
  on_click(event=None)
  post_menu()
  on_select(option)
  update_styles(active_categories=None)

class spacr.gui_elements.spacrCheckbutton(parent, text='', variable=None, command=None, *args, **kwargs)

  Bases: `tkinter.ttk.Checkbutton`

  Ttk Checkbutton widget which is either in on- or off-state.

  text = ''
  variable
  command = None

class spacr.gui_elements.spacrProgressBar(parent, label=True, *args, **kwargs)

  Bases: `tkinter.ttk.Progressbar`

  Ttk Progressbar widget shows the status of a long-running operation. They can operate in two modes: determinate mode shows the amount completed relative to the total amount of work to be done, and indeterminate mode provides an animated display to let the user know that something is happening.

  fg_color = 'white'
  bg_color = 'black'
  active_color = 'blue'
  inactive_color = 'dark_gray'
  font_size = 12
  font_loader
  style
  label = True
  operation_type = None
  additional_info = None
  set_label_position()
  update_label()

class spacr.gui_elements.spacrSlider(master=None, length=None, thickness=2, knob_radius=10, position='center', from_=0, to=100, value=None, show_index=False, command=None, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  specified_length = None
  knob_radius = 10
  thickness = 2
  knob_position = 10
  slider_line = None
  knob = None
  position = ''
  offset = 0
  from_ = 0
  to = 100
  value = None
  show_index = False
  command = None
  fg_color = 'white'
  bg_color = 'black'
  active_color = 'blue'
  inactive_color = 'dark_gray'
  canvas
  length = None
  resize_slider(event)
  value_to_position(value)
  position_to_value(position)
  draw_slider(inactive=False)
  move_knob(event)
  activate_knob(event)
  release_knob(event)
  set_to(new_to)
  get()
  set(value)

    Set the slider’s value and update the knob position.

  jump_to_click(event)
  update_slider_from_entry(event)

    Update the slider’s value from the entry.

spacr.gui_elements.spacrScrollbarStyle(style, inactive_color, active_color)

class spacr.gui_elements.spacrFrame(container, width=None, *args, bg='black', radius=20, scrollbar=True, textbox=False, **kwargs)

  Bases: `tkinter.ttk.Frame`

  Ttk Frame widget is a container, used to group other widgets together.

  inactive_color = 'dark_gray'
  active_color = 'blue'
  fg_color = 'white'
  rounded_rectangle(canvas, x1, y1, x2, y2, radius=20, **kwargs)

class spacr.gui_elements.spacrLabel(parent, text='', font=None, style=None, align='right', height=None, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  text = ''
  align = 'right'
  style_out
  font_style = 'OpenSans'
  font_size = 12
  font_family = 'OpenSans'
  font_loader
  canvas
  style = None
  set_text(text)

class spacr.gui_elements.spacrButton(parent, text='', command=None, font=None, icon_name=None, size=50, show_text=True, outline=False, animation=True, *args, **kwargs)

  Bases: `tkinter.Frame`

  Frame widget which may contain other widgets and can have a 3D border.

  text = ''
  command = None
  icon_name = ''
  size = 50
  show_text = True
  outline = False
  animation = True
  font_size = 12
  font_loader
  canvas
  inactive_color = 'dark_gray'
  bg_color = 'dark_gray'
  active_color = 'blue'
  fg_color = 'white'
  is_zoomed_in = False
  load_icon()
  get_icon_path(icon_name)
  on_enter(event=None)
  on_leave(event=None)
  on_click(event=None)
  create_rounded_rectangle(x1, y1, x2, y2, radius=20, **kwargs)
  update_description(event)
  clear_description(event)
  animate_zoom(target_scale, steps=10, delay=10)
  zoom_icon(scale_factor)

class spacr.gui_elements.spacrSwitch(parent, text='', variable=None, command=None, *args, **kwargs)

  Bases: `tkinter.ttk.Frame`

  Ttk Frame widget is a container, used to group other widgets together.

  text = ''
  variable
  command = None
  canvas
  switch_bg
  switch
  label
  toggle(event=None)
  update_switch()
  animate_switch()
  animate_movement(start_x, end_x, final_color)
  get()
  set(value)
  create_rounded_rectangle(x1, y1, x2, y2, radius=9, **kwargs)

class spacr.gui_elements.spacrToolTip(widget, text)
  widget
  text
  tooltip_window = None
  show_tooltip(event)
  hide_tooltip(event)

class spacr.gui_elements.ModifyMaskApp(root, folder_path, scale_factor)
  root
  folder_path
  scale_factor
  image_filenames
  masks_folder
  current_image_index = 0
  canvas_width
  canvas_height
  update_display()
  update_original_mask_from_zoom()
  update_original_mask(zoomed_mask, x0, x1, y0, y1)
  get_scaling_factors(img_width, img_height, canvas_width, canvas_height)
  canvas_to_image(x_canvas, y_canvas)
  apply_zoom_on_enter(event)
  normalize_image(image, lower_quantile, upper_quantile)
  resize_arrays(img, mask)
  load_first_image()
  setup_canvas()
  initialize_flags()
  update_mouse_info(event)
  setup_navigation_toolbar()
  setup_mode_toolbar()
  setup_function_toolbar()
  setup_zoom_toolbar()
  load_image_and_mask(index)
  display_image()
  display_zoomed_image()
  overlay_mask_on_image(image, mask, alpha=0.5)
  previous_image()
  next_image()
  save_mask()
  set_zoom_rectangle_start(event)
  set_zoom_rectangle_end(event)
  update_zoom_box(event)
  toggle_zoom_mode()
  toggle_brush_mode()
  image_to_canvas(x_image, y_image)
  toggle_dividing_line_mode()
  start_dividing_line(event)
  finish_dividing_line(event)
  update_dividing_line_preview(event)
  apply_dividing_line()
  toggle_draw_mode()
  toggle_magic_wand_mode()
  toggle_erase_mode()
  apply_brush_release(event)
  erase_brush_release(event)
  apply_brush(event)
  erase_brush(event)
  erase_object(event)
  use_magic_wand(event)
  apply_magic_wand(image, mask, seed_point, tolerance, maximum, action='add')
  magic_wand_normal(seed_point, tolerance, action)
  magic_wand_zoomed(seed_point, tolerance, action)
  draw(event)
  draw_on_zoomed_mask(draw_coordinates)
  finish_drawing(event)
  finish_drawing_if_active(event)
  apply_normalization()
  fill_objects()
  relabel_objects()
  clear_objects()
  invert_mask()
  remove_small_objects()

class spacr.gui_elements.AnnotateApp(root, db_path, src, image_type=None, channels=None, image_size=200, annotation_column='annotate', percentiles=(1, 99), measurement=None, threshold=None, normalize_channels=None, outline=None, outline_threshold_factor=1, outline_sigma=1, edge_thickness=1, edge_transparency=100, edge_image=False, object_size=(0, 0))
  root
  db_path
  src
  index = 0
  SENTINEL
  orig_annotation_columns = 'annotate'
  annotation_column = 'annotate'
  image_type = None
  channels = None
  percentiles = (1, 99)
  images
  pending_updates
  labels = []
  adjusted_to_original_paths
  terminate = False
  update_queue
  measurement = None
  threshold = None
  normalize_channels = None
  outline = None
  outline_threshold_factor = 1
  outline_sigma = 1
  edge_thickness = 1
  edge_transparency = 100
  edge_image = False
  object_size = (0, 0)
  font_loader
  font_size = 12
  bg_color = 'black'
  fg_color = 'white'
  active_color = 'blue'
  inactive_color = 'dark_gray'
  worker_busy = False
  filtered_paths_annotations = []
  db_update_thread
  grid_frame
  status_label
  button_frame
  next_button
  previous_button
  exit_button
  settings_button
  clear_button
  count_button
  dl_train_button
  open_umap_window()
  open_settings_window()
  update_settings(**kwargs)
  recreate_image_grid()
  update_display()
  swich_back_annotation_column()
  calculate_grid_dimensions()
  prefilter_paths_annotations()
  load_images()
  show_class_counts()
  load_single_image(path_annotation_tuple)
  static fill_holes(mask, min_size=0)

    Fill holes inside True regions of a binary mask.

    Parameters:

      - mask (ndarray[bool]) – Binary mask where True denotes foreground.

      - min_size (int) – Minimum hole area to fill (in pixels). - <= 0 : fill ALL internal holes. - > 0 : fill only holes smaller than min_size; reopen larger ones.

    Returns:

      Hole-filled mask.

    Return type:

      ndarray[bool]

  outline_image(base_img, full_img, edge_sigma=1, edge_thickness=1, fill_holes=True, object_size=(0, 0))

    Anti-aliased outlines with sub-pixel thickness that never get dimmer as they get thinner. Uses peak normalization so outline brightness is thickness-invariant (then scaled only by edge_transparency).

    Parameters:

      - base_img (PIL.Image) – already filtered (visible base)

      - full_img (PIL.Image) – normalized RGB before filtering (edge detection / underlay)

      - edge_sigma (float) – Gaussian smoothing before thresholding

      - edge_thickness (float) – outline thickness in output pixels (supports < 1, e.g. 0.01)

      - fill_holes (bool) – fill internal holes in foreground masks before boundary extraction

      - object_size (tuple[int,int]) – (min_px, max_px) connected-component area filter. 0 disables that bound.

  static normalize_image(img, percentiles=(1, 99), normalize_channels=None)

    If normalize_channels is None or [], do nothing. Otherwise normalize only those channels (r/g/b).

  add_colored_border(img, border_width, border_color)
  filter_channels(img)
  get_on_image_click(path, label, img)
  static update_html(text)
  clear_current_annotation()
  update_database_worker()
  shutdown()
  next_page()
  previous_page()
  update_gui_text(text)
  train_and_classify()

    1. Merge data from the relevant DB tables (including png_list).

    2) Collect manual annotations from png_list.<annotation_column> => ‘manual_annotation’. - 1 => class=1, 2 => class=0 (for training). 3) If only one class is present, randomly sample unannotated images as the other class. 4) Train an XGBoost model. 5) Classify all rows -> fill XGboost_score (prob of class=1) & XGboost_annotation (1 or 2 if high confidence). 6) Write those columns back to sqlite. 7) Refresh the UI.

  static convert_settings_dict_for_gui(settings)

    Decide widget type per setting: - ‘check’ => Checkbutton (bools) - ‘combo’ => readonly Combobox (predefined choices) - ‘entry’ => Entry (free text / numbers / lists) Returns: {key: (kind, options, initial)}

  build_multi_annotation(source_columns, target_column='multi_annot')

    Consolidate multiple {1,2,NULL} columns into a single integer code column. Unique per combination. If all inputs NULL -> store NULL. Sets self.annotation_column to target_column and refreshes UI.

    Encoding: per col: NULL->0, 1->1, 2->2 code = 1 + sum( digit_i * (3^i) ), i = 0..N-1 (all digits 0) => store NULL instead of 1

  ensure_multi_annot_from_selection(source_columns, target_column='class_column', force_rebuild=True)

    If one column selected -> use it directly (no consolidation). If >=2 selected -> build a consolidated column. If target_column already exists, auto-bump to target_column_1, target_column_2, … and use that actual name everywhere.

    Returns the effective annotation column name.

  open_deep_spacr_window()

spacr.gui_elements.standardize_figure(fig)

spacr.gui_elements.modify_figure_properties(fig, scale_x=None, scale_y=None, line_width=None, font_size=None, x_lim=None, y_lim=None, grid=False, legend=None, title=None, x_label_rotation=None, remove_axes=False, bg_color=None, text_color=None, line_color=None)

  Modifies the properties of the figure, including scaling, line widths, font sizes, axis limits, x-axis label rotation, background color, text color, line color, and other common options.

  Parameters: - fig: The Matplotlib figure object to modify. - scale_x: Scaling factor for the width of subplots (optional). - scale_y: Scaling factor for the height of subplots (optional). - line_width: Desired line width for all lines (optional). - font_size: Desired font size for all text (optional). - x_lim: Tuple specifying the x-axis limits (min, max) (optional). - y_lim: Tuple specifying the y-axis limits (min, max) (optional). - grid: Boolean to add grid lines to the plot (optional). - legend: Boolean to show/hide the legend (optional). - title: String to set as the title of the plot (optional). - x_label_rotation: Angle to rotate the x-axis labels (optional). - remove_axes: Boolean to remove or show the axes labels (optional). - bg_color: Color for the figure and subplot background (optional). - text_color: Color for all text in the figure (optional). - line_color: Color for all lines in the figure (optional).

spacr.gui_elements.save_figure_as_format(fig, file_format)

spacr.gui_elements.modify_figure(fig)

spacr.gui_elements.generate_dna_matrix(output_path='dna_matrix.gif', canvas_width=1500, canvas_height=1000, duration=30, fps=20, base_size=20, transition_frames=30, font_type='arial.ttf', enhance=[1.1, 1.5, 1.2, 1.5], lowercase_prob=0.3)

  Generate a DNA matrix animation and save it as GIF, MP4, or AVI using OpenCV for videos.