[PyQt] Create a start Button

Fabien Lafont lafont.fabien at gmail.com
Wed Dec 21 15:56:52 GMT 2011


Thanks Vincent, it works perfectly now. I think the main error came
from the fact I called CPUMonitor after...

If I remove                   from PyQt4.QtGui import (QApplication,
QLabel, QLineEdit, QSpinBox,
                                                     QVBoxLayout,
QDial, QGridLayout, QComboBox, QPushButton)

it returns an error... I don't understand why either... Because I have
                from PyQt4 import QtCore, QtGui

I need a grid because my soft is not finished yet and I'll add other graphics.

Do you have an idea how can I quit the loop. In fact I want to do a
stop button...

Thanks again!

Fabien

2011/12/21 Vincent Vande Vyvre <vincent.vandevyvre at swing.be>:
> Le 21/12/11 15:36, Fabien Lafont a écrit :
>
> Hello everyone,
>
> I'm trying to create a Start button on my soft. My software just plot
> live datas. When I run the program it display only a pristine graph
> and I want to start the first action of my program when I click on
> start. But If I create this Start button nothing appears...
>
> Why just creating this button block the display of my soft?
>
>
> # -*- coding: utf-8 -*-
> """
> Created on Mon Dec 19 14:51:39 2011
>
> @author: lafont
> """
> #!/usr/bin/env python
>
> from visa import *
> from pylab import *
> import sys
> from PyQt4 import QtGui
> import numpy as np
> import random
> import ImageGrab
> from PyQt4 import QtCore, QtGui
> from matplotlib.figure import Figure
> from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as
> FigureCanvas
> from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg
> as NavigationToolbar
> from PyQt4.QtGui import (QApplication, QLabel, QLineEdit, QSpinBox,
>         QVBoxLayout, QDial, QGridLayout, QComboBox, QPushButton)
> from PyQt4.QtCore import (QObject, Qt, SIGNAL, SLOT)
>
> Your's imports: QtGui + QtCore, QtGui + (QApplication, ...) + (QObject,
> ...),
> make a choice.
>
> QApplication,
>
> #===============================================================================
> #
> #===============================================================================
>
>
> class CPUMonitor(FigureCanvas):
>     def __init__(self,parent):
>
>         self.fig = Figure()
>         self.ax = self.fig.add_subplot(111)
>         FigureCanvas.__init__(self, self.fig)
>         self.ax.set_autoscale_on(True)
>         # generates first "empty" plots
>         self.user, self.nice = [], []
>         self.l_user, = self.ax.plot([], self.user, label='Voltage')
>         self.l_nice, = self.ax.plot([], self.nice, label='Voltage2')
>
>         # force a redraw of the Figure
>         self.fig.canvas.draw()
> #        FigureCanvas.updateGeometry(self)
>
>
>     def action1(self):
>
>         self.result1 = random.randint(0,4)
>         QtCore.QTimer.singleShot(100, self.action2)
>
> You call action2 ...
>
>
>     def action2(self):
>         self.result2 = random.randint(0,1)
>
>         self.user.append(self.result1)
>         self.nice.append(self.result2)
>
>         self.l_user.set_data(range(len(self.user)), self.user)
>         self.l_nice.set_data(range(len(self.nice)), self.nice)
>
>         # force a redraw of the Figure
>         self.fig.canvas.draw()
>         FigureCanvas.updateGeometry(self)
>
>         #envoie de l'évènement ici
>
>         self.action1()
>
> ... and you call action1.
>
> #===============================================================================
> #
> #===============================================================================
>
>
>
> class ApplicationWindow(QtGui.QMainWindow):
>     """Example main window"""
>     def __init__(self):
>         # initialization of Qt MainWindow widget
>         QtGui.QMainWindow.__init__(self)
>         # set window title
>         self.setWindowTitle("QHE manip")
>         # instantiate a widget, it will be the main one
>         self.main_widget = QtGui.QWidget(self)
>         # create a vertical box layout widget
>         vbl = QtGui.QGridLayout(self.main_widget)
>
> I think your GridLayout must be a VBoxLayout, see below
>
>
>         StartButton = QPushButton("START")
>         self.connect(StartButton, SIGNAL('released()'),
> CPUMonitor.action1)
>
> It's preferable to instanciate CPUMonitor() before the button
>
>
>         # instantiate our Matplotlib canvas widget
>         qmc = CPUMonitor(self.main_widget)
>
>         # instantiate the navigation toolbar
>         ntb = NavigationToolbar(qmc, self.main_widget)
>
>         # pack these widget into the vertical box
>
> Move your button here, and use signal new style.
>     startButton = QPushButton("Start")
>     startButton.clicked.connect(qmc.action1)
>
>         vbl.addWidget(qmc,0,0)
>         vbl.addWidget(ntb,1,0)
>         vbl.addWidget(StartButton,4,0)
>
> Raise an error if vbl is a grid layout
>
>
>         # set the focus on the main widget
>         self.main_widget.setFocus()
>
>         # set the central widget of MainWindow to main_widget
>         self.setCentralWidget(self.
> main_widget)
>
> #    def arret(self):
> #        stop = False
> #        print stop
> #    def commence(self):
> #        stop = True
> # create the GUI application
> qApp = QtGui.QApplication(sys.argv)
> # instantiate the ApplicationWindow widget
> aw = ApplicationWindow()
> # show the widget
> aw.show()
> # start the Qt main loop execution, exiting from this script
> # with the same return code of Qt application
> sys.exit(qApp.exec_())
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt
>
> Not tested, I don't use pylab, matplotlib etc
>
> Regards
>
> --
> Vincent V.V.
> Oqapy . Qarte+7 . PaQager
>
> _______________________________________________
> PyQt mailing list    PyQt at riverbankcomputing.com
> http://www.riverbankcomputing.com/mailman/listinfo/pyqt


More information about the PyQt mailing list