Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Dynamic QLabel PyQt4

By: a guest on Mar 9th, 2013  |  syntax: Python  |  size: 2.67 KB  |  views: 32  |  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. class MyTextCanvas(FigureCanvas):
  13.     """Ultimately, this is a QWidget (as well as a FigureCanvasAgg, etc.)."""
  14.     def __init__(self, parent=None, width=5, height=4, dpi=100):
  15.         fig = Figure(figsize=(width, height), dpi=dpi)
  16.         self.axes = fig.add_subplot(111)
  17.         # We want the axes cleared every time plot() is called
  18.         self.axes.hold(False)
  19.         self.compute_initial_figure()
  20.  
  21.         FigureCanvas.__init__(self, fig)
  22.         self.setParent(parent)
  23.  
  24.         FigureCanvas.setSizePolicy(self,
  25.                                    QtGui.QSizePolicy.Expanding,
  26.                                    QtGui.QSizePolicy.Expanding)
  27.         FigureCanvas.updateGeometry(self)
  28.              
  29.         ########### I added these things below
  30.         self.text = 'this is the initial text'
  31.         self.create_initial_text()
  32.         ###########
  33.        
  34.     def compute_initial_figure(self):
  35.         pass
  36.    
  37.     def create_initial_text(self):
  38.         pass
  39.  
  40. class myLabelClass(MyTextCanvas):
  41.     """This is my own creaed class which more or less resembles the code above."""
  42.     def __init__(self, *args, **kwargs):
  43.         MyTextCanvas.__init__(self, *args, **kwargs)
  44.         timer = QtCore.QTimer(self)        
  45.         QtCore.QObject.connect(timer, QtCore.SIGNAL('timeout()'), self.countUp)
  46.         timer.start(1000)
  47.  
  48.     def create_initial_text(self):
  49.         QtGui.QLabel(self.text, self)
  50.    
  51.     def countUp(self):
  52.         for i in xrange(100):
  53.             self.myShinyLabel.setText(str(i))
  54.            
  55.  
  56. class ApplicationWindow(QtGui.QMainWindow):
  57.     def __init__(self):
  58.         QtGui.QMainWindow.__init__(self)
  59.         self.setAttribute(QtCore.Qt.WA_DeleteOnClose)
  60.         self.setWindowTitle("Hielke's programma")
  61.  
  62.         self.main_widget = QtGui.QWidget(self)
  63.  
  64.         layout = QtGui.QHBoxLayout(self.main_widget)
  65.         self.myShinyLabel = myLabelClass(self.main_widget)
  66.         layout.addWidget(self.myShinyLabel)
  67.         #dynamicImage = MyDynamicMplCanvas(self.main_widget, width=5, height=4, dpi=100)
  68.         #layout.addWidget(dynamicImage)
  69.  
  70.         self.main_widget.setFocus()
  71.         self.setCentralWidget(self.main_widget)
  72.  
  73. def main():
  74.     qApp = QtGui.QApplication(sys.argv)
  75.     aw = ApplicationWindow()
  76.     aw.setWindowTitle("%s" % progname)
  77.     aw.show()
  78.     sys.exit(qApp.exec_())
  79.  
  80.  
  81. if __name__ == "__main__":
  82.     main()
clone this paste RAW Paste Data