Skip to content
Merged
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
Binary file added .github/assets/screenshot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
name: CI

on:
push:
branches: [master]
pull_request:
workflow_dispatch:

jobs:
test:
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
python-version: ["3.9", "3.11", "3.12"]

steps:
- uses: actions/checkout@v5

- uses: actions/setup-python@v6
with:
python-version: ${{ matrix.python-version }}
cache: pip

# PyQt5 wheels bundle Qt itself but still link against these system
# libraries, which the GitHub runner image does not ship. They are
# needed even for the offscreen platform plugin.
- name: Install Qt system libraries
run: |
sudo apt-get update
sudo apt-get install -y --no-install-recommends \
libegl1 libgl1 libdbus-1-3 libxkbcommon-x11-0 \
libxcb-icccm4 libxcb-image0 libxcb-keysyms1 libxcb-randr0 \
libxcb-render-util0 libxcb-shape0 libxcb-xfixes0 libxcb-xinerama0

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r requirements.txt
pip install pytest

- name: Run tests
env:
QT_QPA_PLATFORM: offscreen
run: pytest

lint:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v5
- uses: actions/setup-python@v6
with:
python-version: "3.12"
- run: pip install ruff
- run: ruff check .
26 changes: 26 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Byte-compiled / optimised files
__pycache__/
*.py[cod]
*$py.class

# Distribution / packaging
build/
dist/
*.egg-info/
.eggs/

# Virtual environments
.venv/
venv/
env/

# Test / coverage artefacts
.pytest_cache/
.coverage
coverage.xml
htmlcov/

# Editors and OS cruft
.idea/
.vscode/
.DS_Store
150 changes: 60 additions & 90 deletions CommonMPL.py
Original file line number Diff line number Diff line change
@@ -1,113 +1,83 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Pierre Haessig — March 2014
"""
Qt adaptation of Gael Varoquaux's tutorial to integrate Matplotlib
http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html#extending-traitsui-adding-a-matplotlib-figure-to-our-application
based on Qt-based code shared by Didrik Pinte, May 2012
http://markmail.org/message/z3hnoqruk56g2bje
adapted and tested to work with PySide from Anaconda in March 2014
"""
#!/usr/bin/env python3
"""A TraitsUI editor that embeds a matplotlib figure in a Qt5 application.

from pyface.qt import QtGui, QtCore
#from traits.etsconfig.api import ETSConfig
#ETSConfig.toolkit = 'qt4'
#
import matplotlib as mpl
#mpl.rcParams['backend.qt4']='PySide'
# We want matplotlib to use a QT backend
mpl.use('Qt4Agg')
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT

from traits.api import Any, Instance
from traitsui.qt4.editor import Editor
from traitsui.qt4.basic_editor_factory import BasicEditorFactory
from traitsui.api import Handler
Originally a Qt adaptation by Pierre Haessig (March 2014) of Gael Varoquaux's
tutorial on integrating matplotlib with TraitsUI, based on Qt code shared by
Didrik Pinte (May 2012):

* http://docs.enthought.com/traitsui/tutorials/traits_ui_scientific_app.html
* http://markmail.org/message/z3hnoqruk56g2bje
* https://gist.github.com/pierre-haessig/9838326

Import order matters here. ``pyface.qt`` is imported first so that it resolves
and caches the Qt binding (PyQt5, PySide2, ...); matplotlib then reuses the
already-imported binding instead of independently picking a different one,
which would crash the moment the two tried to share a widget.
"""

from traits.api import *
from traitsui.api import View,UItem, Item, Group, Heading, Label, \
HSplit, Handler, CheckListEditor, EnumEditor, TableEditor, FileEditor, \
ListEditor, Tabbed, VGroup, HGroup, RangeEditor, Spring, spring
from traitsui.menu import NoButtons
# isort: off
# Order is deliberate -- see the module docstring. pyface.qt must resolve the
# Qt binding before matplotlib looks for one. Do not let a formatter sort these.
from pyface.qt import QtGui

#from traitsui.wx.editor import Editor
from traitsui.api import ColorTrait
from matplotlib.backends.backend_qtagg import (
FigureCanvasQTAgg as FigureCanvas,
NavigationToolbar2QT as NavigationToolbar,
)

import numpy as np
from matplotlib import *
import os,sys,platform,socket,random
# isort: on

class _MPLFigureEditor(Editor):
from traitsui.api import Handler
from traitsui.basic_editor_factory import BasicEditorFactory
from traitsui.qt.editor import Editor

scrollable = True
__all__ = ["MPLFigureEditor", "MPLInitHandler"]

def init(self, parent):
self.control = self._create_canvas(parent)
self.set_tooltip()

def update_editor(self):
pass
class _MPLFigureEditor(Editor):
"""Wraps a :class:`~matplotlib.figure.Figure` trait in a Qt canvas."""

def _create_canvas(self, parent):
""" Create the MPL canvas. """
# matplotlib commands to create a canvas
frame = QtGui.QWidget()
mpl_canvas = FigureCanvas(self.value)
mpl_canvas.setParent(frame)
mpl_toolbar = NavigationToolbar2QT(mpl_canvas,frame)
scrollable = True

vbox = QtGui.QVBoxLayout()
vbox.addWidget(mpl_canvas)
vbox.addWidget(mpl_toolbar)
frame.setLayout(vbox)
def init(self, parent):
self._canvas = None
self.control = self._create_canvas(parent)
self.set_tooltip()

return frame
def update_editor(self):
"""Redraw the canvas when the underlying figure trait changes."""
if self._canvas is not None:
self._canvas.draw_idle()

class MPLFigureEditor(BasicEditorFactory):
def _create_canvas(self, parent):
"""Build a frame holding the MPL canvas and its navigation toolbar."""
frame = QtGui.QWidget()
mpl_canvas = FigureCanvas(self.value)
mpl_canvas.setParent(frame)
self._canvas = mpl_canvas
mpl_toolbar = NavigationToolbar(mpl_canvas, frame)

klass = _MPLFigureEditor
vbox = QtGui.QVBoxLayout()
vbox.addWidget(mpl_canvas)
vbox.addWidget(mpl_toolbar)
frame.setLayout(vbox)

class MPLInitHandler(Handler):
"""Handler calls mpl_setup() to initialize mpl events"""

def init(self, info):
"""This method gets called after the controls have all been
created but before they are displayed.
"""
info.object.mpl_setup()
return True
return frame

if __name__ == "__main__":
# Create a window to demo the editor
from traits.api import HasTraits
from traitsui.api import View, Item
from numpy import sin, cos, linspace, pi
from matplotlib.widgets import RectangleSelector

class Test(HasTraits):
class MPLFigureEditor(BasicEditorFactory):
"""Editor factory: ``Item('figure', editor=MPLFigureEditor())``."""

figure = Instance(Figure, ())
klass = _MPLFigureEditor

view = View(Item('figure', editor=MPLFigureEditor(),
show_label=False),
handler=MPLInitHandler,
resizable=True)

def __init__(self):
super(Test, self).__init__()
self.axes = self.figure.add_subplot(111)
t = linspace(0, 2*pi, 200)
self.axes.plot(sin(t)*(1+0.5*cos(11*t)), cos(t)*(1+0.5*cos(11*t)))
class MPLInitHandler(Handler):
"""Calls ``mpl_setup()`` on the view object once its controls exist.

def mpl_setup(self):
def onselect(eclick, erelease):
print("eclick: {}, erelease: {}".format(eclick,erelease))

self.rs = RectangleSelector(self.axes, onselect,
drawtype='box',useblit=True)
Attach this to a ``View`` when you need to register matplotlib event
handlers (pickers, selectors) that require a live canvas.
"""

Test().configure_traits()
def init(self, info):
info.object.mpl_setup()
return True
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 Brendan Griffen

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 0 additions & 48 deletions Python3GUITemplate_macosx.yml

This file was deleted.

69 changes: 0 additions & 69 deletions Python3GUITemplate_ubuntu.yml

This file was deleted.

Binary file removed PythonGUI_mac.png
Binary file not shown.
Binary file removed PythonGUI_ubuntu.png
Binary file not shown.
Loading