<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta content="text/html; charset=ISO-8859-1"
http-equiv="Content-Type">
<title></title>
</head>
<body text="#000000" bgcolor="#ffffff">
Le 21/12/11 15:36, Fabien Lafont a écrit :
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">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)
</pre>
</blockquote>
Your's imports: QtGui + QtCore, QtGui + (QApplication, ...) +
(QObject, ...),<br>
make a choice. <br>
<pre wrap="">QApplication,</pre>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">#===============================================================================
#
#===============================================================================
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)
</pre>
</blockquote>
You call action2 ...<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
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()
</pre>
</blockquote>
... and you call action1.<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
#===============================================================================
#
#===============================================================================
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)
</pre>
</blockquote>
I think your GridLayout must be a VBoxLayout, see below<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
StartButton = QPushButton("START")
self.connect(StartButton, SIGNAL('released()'),
CPUMonitor.action1)
</pre>
</blockquote>
It's preferable to instanciate CPUMonitor() before the button<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
# 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
</pre>
</blockquote>
Move your button here, and use signal new style.<br>
startButton = QPushButton("Start")<br>
startButton.clicked.connect(qmc.action1)<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
vbl.addWidget(qmc,0,0)
vbl.addWidget(ntb,1,0)
vbl.addWidget(StartButton,4,0)
</pre>
</blockquote>
Raise an error if vbl is a grid layout<br>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap="">
# set the focus on the main widget
self.main_widget.setFocus()
</pre>
</blockquote>
<blockquote
cite="mid:CAC9H_cjuiWeJme83CRpZZYt65HGiwoS5Skrikt+BqEkdBZYeGQ@mail.gmail.com"
type="cite">
<pre wrap=""> # 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 <a class="moz-txt-link-abbreviated" href="mailto:PyQt@riverbankcomputing.com">PyQt@riverbankcomputing.com</a>
<a class="moz-txt-link-freetext" href="http://www.riverbankcomputing.com/mailman/listinfo/pyqt">http://www.riverbankcomputing.com/mailman/listinfo/pyqt</a>
</pre>
</blockquote>
Not tested, I don't use pylab, matplotlib etc<br>
<br>
Regards<br>
<br>
<div class="moz-signature">-- <br>
Vincent V.V.<br>
<a href="https://launchpad.net/oqapy">Oqapy</a> . <a
href="https://launchpad.net/qarte+7">Qarte+7</a> . <a
href="https://launchpad.net/paqager">PaQager</a></div>
</body>
</html>