Created
April 18, 2014 16:21
-
-
Save jbmohler/11052384 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from PySide import QtCore, QtGui | |
| import matplotlib | |
| import matplotlib.widgets as widgets | |
| matplotlib.use('Qt4Agg') | |
| from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas | |
| from matplotlib.figure import Figure | |
| class GraphWindow(QtGui.QMainWindow): | |
| def __init__(self, parent=None): | |
| super(GraphWindow, self).__init__(parent) | |
| self.tb = QtGui.QToolBar() | |
| self.act1 = self.tb.addAction('h') | |
| self.act1.setCheckable(True) | |
| self.act1.setChecked(True) | |
| self.act2 = self.tb.addAction('v') | |
| self.act2.setCheckable(True) | |
| self.group = QtGui.QActionGroup(self) | |
| self.group.addAction(self.act1) | |
| self.group.addAction(self.act2) | |
| self.addToolBar(self.tb) | |
| self.figure = Figure() | |
| self.canvas = FigureCanvas(self.figure) | |
| self.ax = self.figure.add_subplot(1, 1, 1) | |
| self.canvas.mpl_connect('button_press_event', self.on_press) | |
| self.active_tool = None | |
| self.setCentralWidget(self.canvas) | |
| def on_press(self, event): | |
| if self.active_tool != None: | |
| self.active_tool.disconnect_events() | |
| if self.group.checkedAction() == self.act1: | |
| self.active_tool = widgets.SpanSelector(self.ax, self.on_done, 'horizontal') | |
| if self.group.checkedAction() == self.act2: | |
| self.active_tool = widgets.SpanSelector(self.ax, self.on_done, 'vertical') | |
| self.active_tool.press(event) | |
| def on_done(self, *args): | |
| print args | |
| if __name__ == '__main__': | |
| app = QtGui.QApplication([]) | |
| m = GraphWindow() | |
| m.show() | |
| app.exec_() |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you clicked and drag in the axes twice, you'll receive the following traceback. It comes since you've disconnected an event and killed the reference for the handle in an event handler. The cleanup code in cbook caches the entire list to not do even worse things when the list changes during the iteration.
Traceback (most recent call last):
File "c:\system\python27\site-packages\matplotlib\backends\backend_qt4.py", line 257, in mousePressEvent
FigureCanvasBase.button_press_event(self, x, y, button)
File "c:\system\python27\site-packages\matplotlib\backend_bases.py", line 1868, in button_press_event
self.callbacks.process(s, mouseevent)
File "c:\system\python27\site-packages\matplotlib\cbook.py", line 531, in process
del self.callbacks[s][cid]
KeyError: 17