Visualization¶
Classes for plotting, drawing, and displaying images and video.
Figures¶
- class mytk.figures.Figure(figure=None, figsize=None)[source]¶
Bases:
BaseWrapper around matplotlib Figure for embedding plots in tkinter.
- property figure¶
The underlying matplotlib Figure.
- property first_axis¶
The first axes of the figure, or None if unavailable.
- property axes¶
All axes of the figure, or None if the figure is not set.
CanvasView¶
- class mytk.canvasview.CanvasView(width=200, height=200, **kwargs)[source]¶
Bases:
BaseTkinter Canvas wrapper with coordinate system support.
- property relative_basis¶
A dynamic basis that tracks the current canvas size.
- property is_disabled¶
Whether the canvas and its children are disabled.
- class mytk.canvasview.CanvasElement(basis=None, **kwargs)[source]¶
Bases:
objectBase class for drawable elements on a CanvasView.
- property coords¶
The current coordinates of this element on the canvas.
- property tags¶
The tags associated with this element on the canvas.
- class mytk.canvasview.Rectangle(size: (<class 'int'>, <class 'int'>), basis=None, position_is_center=True, **kwargs)[source]¶
Bases:
CanvasElementA rectangle element for drawing on a CanvasView.
- class mytk.canvasview.Oval(size: (<class 'int'>, <class 'int'>), basis=None, position_is_center=True, **kwargs)[source]¶
Bases:
CanvasElementAn oval element for drawing on a CanvasView.
- class mytk.canvasview.Line(points=None, basis=None, **kwargs)[source]¶
Bases:
CanvasElementA line element connecting a series of points on a CanvasView.
- class mytk.canvasview.Arrow(start=None, end=None, **kwargs)[source]¶
Bases:
LineA line with an arrowhead at the end.
- class mytk.canvasview.CanvasLabel(font_size=20, basis=None, **kwargs)[source]¶
Bases:
CanvasElementA text label element for drawing on a CanvasView.
- class mytk.canvasview.Arc(radius, basis=None, **kwargs)[source]¶
Bases:
CanvasElementAn arc element for drawing on a CanvasView.
Data Views¶
- class mytk.dataviews.DataPoint(size: float, **kwargs)[source]¶
Bases:
OvalA circular data point drawn as an oval on a canvas.
- class mytk.dataviews.Function(fct, xs, basis=None, **kwarg)[source]¶
Bases:
CanvasElementA canvas element that draws a mathematical function as a polyline.
- class mytk.dataviews.XYCoordinateSystemElement(size=None, normalized_size=None, axes_limits=((0, 1), (0, 1)), **kwargs)[source]¶
Bases:
CanvasElementAn XY coordinate system with axes, ticks, and labels drawn on a canvas.
- property basis¶
Compute the coordinate basis from axes limits and canvas size.
Images¶
- class mytk.images.Image(filepath=None, url=None, pil_image=None)[source]¶
Bases:
BaseWidget for displaying a PIL image in a tkinter label.
- property width¶
The width of the source PIL image in pixels.
- property height¶
The height of the source PIL image in pixels.
- property displayed_width¶
The width of the currently displayed image in pixels.
- property displayed_height¶
The height of the currently displayed image in pixels.
- observed_property_changed(observed_object, observed_property_name, new_value, context)[source]¶
Handle changes to observed properties such as is_rescalable.
- event_resized(event)[source]¶
Resize the image if is_rescalable, throttling to avoid infinite loops.
- class mytk.images.ImageWithGrid(filepath=None, url=None, pil_image=None)[source]¶
Bases:
ImageImage widget with an optional grid overlay drawn on top.
- class mytk.images.DynamicImage(width=200, height=200)[source]¶
Bases:
CanvasViewCanvas-based image that can be redrawn dynamically.
VideoView¶
- class mytk.videoview.VideoView(device=0, zoom_level=3, auto_start=True)[source]¶
Bases:
BaseLive video capture and display widget backed by OpenCV.
- is_environment_valid()[source]¶
Check that OpenCV and Pillow are available, installing them if needed.
- classmethod available_devices()[source]¶
Return a list of indices for available video capture devices.
- property is_running¶
Return True if video capture is currently active.
- property startstop_button_label¶
Return the appropriate label for the start/stop button.
- update_display(readonly_frame=None)[source]¶
Read the next frame, update the display widget, and schedule the next refresh.
- create_behaviour_buttons()[source]¶
Create start/stop, save, and stream buttons with their callbacks.
- bind_button_to_startstop_behaviour(button)[source]¶
Bind a button to toggle video capture on and off.
- bind_button_to_save_behaviour(button)[source]¶
Bind a button to save the current frame as an image file.
- bind_button_to_stream_behaviour(button)[source]¶
Bind a button to start streaming frames to a video file.
- bind_popup_to_camera_selection_behaviour(popup)[source]¶
Bind a popup menu to switch the active camera device.
- click_start_stop_button(event, button)[source]¶
Toggle capture and update the button label accordingly.
- click_save_button(event, button)[source]¶
Prompt the user for a filename and save the current frame.
Indicators¶
- class mytk.indicators.NumericIndicator(value_variable=None, value=0, format_string='{0}')[source]¶
Bases:
BaseA label that displays a formatted numeric value from a tkinter variable.
- class mytk.indicators.BooleanIndicator(diameter=15)[source]¶
Bases:
CanvasViewA colored circle indicator that is green when True and red when False.
- class mytk.indicators.Level(maximum=100, width=200, height=20)[source]¶
Bases:
CanvasViewA horizontal bar indicator that fills proportionally to a value.
View3D¶
Embedded 3D mesh viewers for myTk: one widget, two rendering backends.
View3D is an abstract widget that loads a GLB/GLTF/OBJ/PLY file with trimesh and shows it lit and coloured inside an ordinary myTk layout — drag to orbit, scroll to zoom. Rather than embed a fragile OpenGL widget, it renders off-screen and blits each frame into a tk.Canvas via Pillow, the same strategy VideoView uses for camera frames. It re-renders only on interaction.
Two concrete implementations share that machinery:
View3DModernGL— a hand-written moderngl renderer (its own shaders, matrices and buffers). Near-zero install footprint, and it uses its own GL bindings rather than PyOpenGL, so it works where pyrender’s context fails:pip install moderngl trimesh numpy Pillow
View3DPyrender— delegates the GL work (shaders, lighting, materials, framebuffer) to pyrender, so the widget keeps almost no GL of its own. Heavier dependencies:pip install pyrender trimesh numpy Pillow
Both backends map UV image textures onto the mesh (else per-vertex/material colour) and honour the object’s own alpha.
Everything that is not backend-specific — the Tk widget and mouse handling, the orbit camera, render scheduling, file loading and the blit — lives in the abstract base. A backend only has to know how to build its renderer, upload the geometry, and draw one frame into a Pillow image.
The base shows static geometry only (no baked animations): textured or per-vertex-coloured triangles with a two-sided Lambert-ish shade — enough to inspect exported scenes.
- class mytk.view3d.View3D(*args, **kwargs)[source]¶
-
Off-screen 3D mesh viewer blitted into a Tk label.
Load a mesh with
load_file(), place it like any other myTk widget, then drag to orbit, scroll to zoom, and Shift+drag (or Shift+scroll) to pan. Renders only on interaction, so it is cheap when idle.Calling
View3D(...)does not build a base instance — it is a factory that picks a rendering backend and returns one of its concrete subclasses, preferringView3DModernGL(lighter deps, its own GL bindings, so it works where pyrender’s PyOpenGL context fails) and falling back toView3DPyrenderonly when moderngl is not importable. Both backends map UV image textures onto the mesh. Ask for a specific backend — e.g. pyrender for its nicer lighting — by instantiating that subclass directly.The choice is made from which backend module imports. moderngl is preferred because pyrender can import yet still fail to create a GL context at render time (notably PyOpenGL on newer Pythons), which the import-probe cannot detect; construct
View3DPyrenderexplicitly when you want it.- Parameters:
A mesh’s own per-vertex/material alpha is honoured, so translucent geometry is blended over the background.
- is_environment_valid()[source]¶
Check that trimesh, numpy, Pillow and the backend module are present.
- create_widget(master)[source]¶
Create the canvas that displays rendered frames and wire up the mouse.
A
tk.Canvas(not a label) is used on purpose: a label sizes itself to its image, so each rendered frame would grow the widget by its chrome and re-trigger<Configure>— a runaway resize loop when the widget is content-sized. A canvas keeps the size the layout gives it regardless of what is drawn into it, so no feedback occurs.
- load_file(path)[source]¶
Load a GLB/GLTF/OBJ/PLY file and display it.
All meshes in the file are handed to the backend, each keeping its own colour (vertex colours, else the material colour, else grey).
Raises whatever trimesh raises for an unreadable/unknown file; use
load_file_or_warn()for the interactive (drop) case.
- load_file_or_warn(path)[source]¶
Load a file, popping up a warning dialog if it is not a usable mesh.
Unlike
load_file(), this never raises — it is meant for dropped or user-picked files, where an unrecognised format should be reported in the UI rather than crash the app. Returns True if the file loaded.
- class mytk.view3d.View3DModernGL(*args, **kwargs)[source]¶
Bases:
View3DView3D backed by a hand-written moderngl renderer.
Owns its own shader program, perspective/look-at matrices and vertex/index buffers, drawing into a standalone off-screen framebuffer.
- set_geometry(interleaved, faces, center, radius)[source]¶
Display a single untextured mesh from raw buffers.
A convenience for vertex-coloured geometry; textured meshes arrive through
_ingest()instead. The buffer is wrapped as one draw item with zero UVs and no texture.- Parameters:
interleaved – Nx10 float32 array of [position, normal, rgba] per vertex (the alpha channel carries the object’s transparency).
faces – Mx3 int32 array of triangle vertex indices.
center – 3-vector at the centre of the geometry’s bounding box.
radius – Half the bounding-box diagonal; frames the orbit camera.
- class mytk.view3d.View3DPyrender(*args, **kwargs)[source]¶
Bases:
View3DView3D backed by pyrender.
pyrender owns the GL context, shaders, lighting, materials and framebuffer, so this backend keeps only a scene graph, an off-screen renderer, and the orbit-camera pose. A mesh’s own alpha is honoured via
alphaMode='BLEND'.
JSON Canvas¶
Viewer for JSON Canvas 1.0 documents (https://jsoncanvas.org/spec/1.0/).
- class mytk.jsoncanvas.JSONCanvas(width=800, height=600, **kwargs)[source]¶
Bases:
CanvasViewRender a JSON Canvas 1.0 document on a Tk canvas.
- Usage:
canvas = JSONCanvas(width=800, height=600) canvas.grid_into(parent, …) canvas.load(document_dict) # or: canvas.load_from_file(“example.canvas”)