Want more features on Pastebin? Sign Up, it's FREE!
Guest

Dynamic QLabel in PyQt4

By: a guest on Mar 9th, 2013  |  syntax: Python  |  size: 3.55 KB  |  views: 30  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #!/usr/bin/env python
  2. from __future__ import unicode_literals
  3. import sys, os, random
  4. from PyQt4 import QtGui, QtCore
  5.  
  6. from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
  7. from matplotlib.figure import Figure
  8.  
  9. progname = os.path.basename(sys.argv[0])
  10. progversion = "0.1"
  11.  
  12.  
  13. class MyMplCanvas(FigureCanvas):
  14.     """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
  15.     def __init__(self, parent=None, width=5, height=4, dpi=100):
  16.         fig = Figure(figsize=(width, height), dpi=dpi)
  17.         self.axes = fig.add_subplot(111)
  18.         # We want the axes cleared every time plot() is called
  19.         self.axes.hold(False)
  20.         self.compute_initial_figure()
  21.  
  22.         FigureCanvas.__init__(self, fig)
  23.         self.setParent(parent)
  24.  
  25.         FigureCanvas.setSizePolicy(self,
  26.                                    QtGui.QSizePolicy.Expanding,
  27.                                    QtGui.QSizePolicy.Expanding)
  28.         FigureCanvas.updateGeometry(self)
  29.              
  30.         ########### I added these things below
  31.         self.text = 'this is the initial text'
  32.         self.create_initial_text()
  33.         ###########
  34.        
  35.     def compute_initial_figure(self):
  36.         pass
  37.    
  38.     def create_initial_text(self):
  39.         pass
  40.  
  41. class MyDynamicMplCanvas(MyMplCanvas):
  42.     """A canvas that updates itself every second with a new plot."""
  43.     def __init__(self, *args, **kwargs):
  44.         MyMplCanvas.__init__(self, *args, **kwargs)
  45.         timer = QtCore.QTimer(self)
  46.         QtCore.QObject.connect(timer, QtCore.SIGNAL("timeout()"), self.update_figure)
  47.         timer.start(1000)
  48.        
  49.     def compute_initial_figure(self):
  50.          self.axes.plot([0, 1, 2, 3], [1, 2, 3, 4], 'r')
  51.  
  52.     def update_figure(self):
  53.         # Build a list of 4 random integers between 0 and 10 (both inclusive)
  54.         l = [ random.randint(0, 10) for i in range(4) ]
  55.  
  56.         self.axes.plot([0, 1, 2, 3], l, 'r')
  57.         self.draw()
  58.    
  59. class myLabelClass(MyMplCanvas):
  60.     """This is my own creaed class which more or less resembles the code above."""
  61.     def __init__(self, *args, **kwargs):
  62.         MyMplCanvas.__init__(self, *args, **kwargs)
  63.         timer = QtCore.QTimer(self)        
  64.         QtCore.QObject.connect(timer, QtCore.SIGNAL('timeout()'), self.countUp)
  65.         timer.start(1000)
  66.  
  67.     def create_initial_text(self):
  68.         QtGui.QLabel(self.text, self)
  69.    
  70.     def countUp(self):
  71.         for i in xrange(100):
  72.             self.myShinyLabel.setText(str(i))
  73.            
  74.  
  75. class ApplicationWindow(QtGui.QMainWindow):
  76.     def __init__(self):
  77.         QtGui.QMainWindow.__init__(self)
  78.         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  79.         self.setWindowTitle("Hielke's programma")
  80.  
  81.         self.main_widget = QtGui.QWidget(self)
  82.  
  83.         layout = QtGui.QHBoxLayout(self.main_widget)
  84.         self.myShinyLabel = myLabelClass(self.main_widget)
  85.         layout.addWidget(self.myShinyLabel)
  86.         dynamicImage = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
  87.         layout.addWidget(dynamicImage)
  88.  
  89.         self.main_widget.setFocus()
  90.         self.setCentralWidget(self.main_widget)
  91.  
  92.         self.statusBar().showMessage("All hail matplotlib!", 2000)
  93.        
  94.        
  95.     def fileQuit(self):
  96.         self.close()
  97.  
  98.     def closeEvent(self, ce):
  99.         self.fileQuit()
  100.  
  101. def main():
  102.     qApp = QtGui.QApplication(sys.argv)
  103.     aw = ApplicationWindow()
  104.     aw.setWindowTitle("%s" % progname)
  105.     aw.show()
  106.     sys.exit(qApp.exec_())
  107.  
  108.  
  109. if __name__ == "__main__":
  110.     main()
clone this paste RAW Paste Data