#!/usr/bin/python
# coding: Latin-1 -*-
import sys
from qt import *
from pylab import *
from matplotlib.backends.backend_qtagg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
class QtAppli(QApplication):
"Fenêtre de l'application"
# Constructeur fenêtre
def __init__(self,
argv):
# Appel constructeur de l'objet hértié
QApplication.__init__(self, argv)
# Attributs de l'application
self.argv=argv
# Widget principale
self.wid=QMainWindow()
self.setMainWidget(self.wid)
self.wid.setCentralWidget(QWidget(self.wid))
self.wid.statusBar()
# Titre
self.wid.setCaption("toto" )
# Un espace de rangement
box=QVBoxLayout(self.wid.centralWidget())
# Le bouton n° 1
btn1=QPushButton(self.wid.centralWidget())
btn1.setText("Essai Qt" )
self.connect(btn1, SIGNAL("clicked()" ), self.slotQt)
box.addWidget(btn1)
# Le bouton n° 2
btn2=QPushButton(self.wid.centralWidget())
btn2.setText("Essai MatPlotLib" )
self.connect(btn2, SIGNAL("clicked()" ), self.slotMatPlotLib)
box.addWidget(btn2)
# Affichage et lancement application
def run(self):
self.wid.show()
self.exec_loop()
# Slot qui affiche une fenêtre Qt avec un module matplotlib
def slotQt(self):
print "clicked qt"
graph=QtMatPlotLib(self.wid.centralWidget(), "GraphQT" )
graph.show()
# Slot qui appelle directement matplotlib
def slotMatPlotLib(self):
print "clicked matplotlib"
x=arange(0, 100, 0.01)
lab=subplot(111)
lab.g=plot(x, sqrt(x))
matplotlib.interactive(True)
show()
# Objet pour gérer la fenêtre Qt avec matplotlib
class QtMatPlotLib(QDialog):
def __init__(self,parent,titre, modal = 0,fl = 0):
QDialog.__init__(self,parent,titre,modal,fl)
self.setName(titre)
self.setCaption("QtMatPlotLib" )
self.Layout = QVBoxLayout(self)
self.Layout.setGeometry(QRect(30,30,531,331))
self.fig=Figure(figsize=(5, 4), dpi=100)
self.sub=self.fig.add_subplot(111)
t=arange(0, 100, 0.1)
self.l1,=self.sub.plot(t, sqrt(t))
self.l2,=self.sub.plot(t, sin(t))
can=FigureCanvas(self.fig)
can.reparent(self, QPoint(0, 0))
self.Layout.addWidget(can)
btn1=QPushButton(self)
btn1.setText("ligne 1" )
self.connect(btn1, SIGNAL("clicked()" ), self.changel1)
self.Layout.addWidget(btn1)
btn2=QPushButton(self)
btn2.setText("ligne 2" )
self.connect(btn2, SIGNAL("clicked()" ), self.changel2)
self.Layout.addWidget(btn2)
self.resize(QSize(600, 300).expandedTo(self.minimumSizeHint()))
def changel1(self):
print "clicked l1"
self.l1.set_visible(not self.l1.get_visible())
self.sub.draw()
def changel2(self):
print "clicked l2"
self.l2.set_visible(not self.l2.get_visible())
self.sub.draw()
def __del__(self):
print "QMatPlotLib deleted"
Appli=QtAppli(sys.argv)
Appli.run()