Re: [PyQt] PyQt Digest, Vol 101, Issue 21
This seems to work (but I don't think its thread safe). You probably need to use a mutex or semaphore somewhere. This demo demonstrates how to embed a matplotlib (mpl) plot into a PyQt4 GUI application, including: * Using the navigation toolbar * Adding data to the plot * Dynamically modifying the plot's properties * Processing mpl events * Saving the plot to a file from a menu The main goal is to serve as a basis for developing rich PyQt GUI applications featuring mpl plots (using the mpl OO API). Eli Bendersky (eli...@gmail.com) License: this code is in the public domain Last modified: 19.01.2009 import sys, os, random import time from PyQt4.QtCore import * from PyQt4.QtGui import * #from PyQt4.QtCore import QThread import matplotlib from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar from matplotlib.figure import Figure class AppForm(QMainWindow): def __init__(self, parent=None): QMainWindow.__init__(self, parent) self.setWindowTitle('Demo: PyQt with matplotlib') self.create_menu() self.create_main_frame() self.create_status_bar() self.textbox.setText('1 2 3 4') self.on_draw() def save_plot(self): file_choices = PNG (*.png)|*.png path = unicode(QFileDialog.getSaveFileName(self, 'Save file', '', file_choices)) if path: self.canvas.print_figure(path, dpi=self.dpi) self.statusBar().showMessage('Saved to %s' % path, 2000) def on_about(self): msg = A demo of using PyQt with matplotlib: * Use the matplotlib navigation bar * Add values to the text box and press Enter (or click Draw) * Show or hide the grid * Drag the slider to modify the width of the bars * Save the plot to a file using the File menu * Click on a bar to receive an informative message QMessageBox.about(self, About the demo, msg.strip()) def on_pick(self, event): # The event received here is of the type # matplotlib.backend_bases.PickEvent # # It carries lots of information, of which we're using # only a small amount here. # box_points = event.artist.get_bbox().get_points() msg = You've clicked on a bar with coords:\n %s % box_points QMessageBox.information(self, Click!, msg) def on_draw(self): Redraws the figure str = unicode(self.textbox.text()) self.data = map(int, str.split()) x = range(len(self.data)) # clear the axes and redraw the plot anew # self.axes.clear() self.axes.grid(self.grid_cb.isChecked()) self.axes.bar( left=x, height=self.data, width=self.slider.value() / 100.0, align='center', alpha=0.44, picker=5) self.canvas.draw() def create_main_frame(self): self.main_frame = QWidget() # Create the mpl Figure and FigCanvas objects. # 5x4 inches, 100 dots-per-inch # self.dpi = 100 self.fig = Figure((5.0, 4.0), dpi=self.dpi) self.canvas = FigureCanvas(self.fig) self.canvas.setParent(self.main_frame) # Since we have only one plot, we can use add_axes # instead of add_subplot, but then the subplot # configuration tool in the navigation toolbar wouldn't # work. # self.axes = self.fig.add_subplot(111) # Bind the 'pick' event for clicking on one of the bars # self.canvas.mpl_connect('pick_event', self.on_pick) # Create the navigation toolbar, tied to the canvas # self.mpl_toolbar = NavigationToolbar(self.canvas, self.main_frame) # Other GUI controls # self.textbox = QLineEdit() self.textbox.setMinimumWidth(200) self.connect(self.textbox, SIGNAL('editingFinished ()'), self.on_draw) self.draw_button = QPushButton(Draw) self.connect(self.draw_button, SIGNAL('clicked()'), self.on_draw) self.grid_cb = QCheckBox(Show Grid) self.grid_cb.setChecked(False) self.connect(self.grid_cb, SIGNAL('stateChanged(int)'), self.on_draw) slider_label = QLabel('Bar width (%):') self.slider = QSlider(Qt.Horizontal) self.slider.setRange(1, 100) self.slider.setValue(20) self.slider.setTracking(True) self.slider.setTickPosition(QSlider.TicksBothSides) self.connect(self.slider, SIGNAL('valueChanged(int)'), self.on_draw) # # Layout with box sizers # hbox =
[PyQt] Error when compiling Py in Eric4
I received this reply to a bug I reported to the Eric 4 guys. When I add the QtPlot widget in Qt to a Mainwindow dialog box it causes their Dialog Compiler to crash. Hi, this is a PyQt4 issue. You should report it (together with a minimal example) on the PyQt4 mailing list. Regards, Detlev On Samstag, 5. Juni 2010, you wrote: This error is apparently caused by the addition of a QtPlot widget. When I removed it the error went away. Version Numbers: Python 2.6.4 Qt 4.5.2 PyQt4 4.5.4 sip 4.8.2 QScintilla 2.4 eric4 4.4.4a (r3717) Platform: win32 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] Plugins Version Numbers: PluginAbout 4.4.0 PluginEricapi 4.4.0 PluginEricdoc 4.4.0 PluginProjectPylons 1.0.0 PluginProjectWxPython 1.0.3 PluginSyntaxChecker 4.4.0 PluginTabnanny 4.4.0 PluginVcsPySvn 4.4.0 PluginVcsSubversion 4.4.0 PluginVmListspace 4.4.0 PluginVmMdiArea 4.4.0 PluginVmTabview 4.4.0 PluginVmWorkspace 4.4.0 PluginWizardPyRegExp 4.4.0 PluginWizardQColorDialog 4.4.0 PluginWizardQFileDialog 4.4.0 PluginWizardQFontDialog 4.4.0 PluginWizardQInputDialog 4.4.0 PluginWizardQMessageBox 4.4.0 PluginWizardQRegExp 4.4.0 CONTENTS OF ERROR LOG HERE v 2010-06-06, 01:48:30 type 'exceptions.TypeError': 'set' object does not support indexing File C:\Python26\Lib\site-packages\eric4\Project\ProjectFormsBrowser.py, line 815, in __generateDialogCode dlg = CreateDialogCodeDialog(fn, self.project, self) File C:\Python26\Lib\site-packages\eric4\Project\CreateDialogCodeDialog.py, line 99, in __init__ self.__updateSlotsModel() File C:\Python26\Lib\site-packages\eric4\Project\CreateDialogCodeDialog.py, line 170, in __updateSlotsModel dlg = uic.loadUi(self.formFile) File C:\Python26\lib\site-packages\PyQt4\uic\__init__.py, line 112, in loadUi return DynamicUILoader().loadUi(uifile, baseinstance) File C:\Python26\lib\site-packages\PyQt4\uic\Loader\loader.py, line 21, in loadUi return self.parse(filename) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 768, in parse actor(elem) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 616, in createUserInterface self.traverseWidgetTree(elem) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 594, in traverseWidgetTree handler(self, child) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 168, in createWidget self.traverseWidgetTree(elem) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 594, in traverseWidgetTree handler(self, child) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 162, in createWidget self.stack.push(self.setupObject(widgetClass(elem), parent, elem)) File C:\Python26\lib\site-packages\PyQt4\uic\uiparser.py, line 134, in setupObject obj = self.factory.createQObject(clsname, name, args, is_attribute) File C:\Python26\lib\site-packages\PyQt4\uic\objcreator.py, line 57, in createQObject classType = self.findQObjectType(classname) File C:\Python26\lib\site-packages\PyQt4\uic\objcreator.py, line 67, in findQObjectType w = module.search(classname) File C:\Python26\lib\site-packages\PyQt4\uic\Loader\qobjectcreator.py, line 24, in search self._module = __import__(self._moduleName, {}, {}, self._classes) Version Numbers: Python 2.6.4 Qt 4.5.2 PyQt4 4.5.4 sip 4.8.2 QScintilla 2.4 eric4 4.4.4a (r3717) Platform: win32 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] Plugins Version Numbers: PluginAbout 4.4.0 PluginEricapi 4.4.0 PluginEricdoc 4.4.0 PluginProjectPylons 1.0.0 PluginProjectWxPython 1.0.3 PluginSyntaxChecker 4.4.0 PluginTabnanny 4.4.0 PluginVcsPySvn 4.4.0 PluginVcsSubversion 4.4.0 PluginVmListspace 4.4.0 PluginVmMdiArea 4.4.0 PluginVmTabview 4.4.0 PluginVmWorkspace 4.4.0 PluginWizardPyRegExp 4.4.0 PluginWizardQColorDialog 4.4.0 PluginWizardQFileDialog 4.4.0 PluginWizardQFontDialog 4.4.0 PluginWizardQInputDialog 4.4.0 PluginWizardQMessageBox 4.4.0 PluginWizardQRegExp 4.4.0 ___ PyQt mailing listPyQt@riverbankcomputing.com http://www.riverbankcomputing.com/mailman/listinfo/pyqt