PyQt - clear widget for redraw

Ken Godee ken at
Wed Aug 4 00:38:25 CEST 2004

Peter wrote:

> Yes - tried that, but gives incomplete draw / flicker.
> More details:
> the idea is to provide a graphics demo of sort algorithms for my 13 year old
> son.  A number of lines of differring lengths are shown on the screen, and
> you can watch as these are sorted, step by step.  So you can see the
> difference between bubble sort, ripple sort, etc.
> Code copied below.
> Improving the smoothness of the display is the main thing I'm after.

>     def refresh(self):
>         self.LCDcount.display('%3d' % cComp)    # update counters
>         self.LCDswap.display('%3d' % cSwap)
>         self.frame4.erase()                      # clear any old lines
>         p = QPainter(self.frame4)
>         mar = self.frame4.width()/100            # margin
>         vsp = 2                                  # vertical spacing
>         hrz = (self.frame4.width()-2*mar)/100    # horizontal scalar
>         p.setPen(QColor("black"))
>         for i in range(100):
>             p.drawLine(mar,mar+vsp*i,hrz*(l1[i]+1),mar+vsp*i)
>         p.setPen(QColor("blue"))
>         p.drawLine(mar,mar+vsp*b1-1,hrz*100,mar+vsp*b1-1)
>         p.drawLine(mar,mar+vsp*b2-1,hrz*100,mar+vsp*b2-1)
>         p.setPen(QColor("red"))
>         p.drawLine(mar,mar+vsp*r1+1,hrz*100,mar+vsp*r1+1)
>         p.drawLine(mar,mar+vsp*r2+1,hrz*100,mar+vsp*r2+1)

To me it would seem the problems lies in the refresh routine,
you're erasing and then redrawing, a viewed object, this will
cause it to flicker around, even worse if using sizers.
There's probally a better way to do this, but when I've
been faced with this type of problem here's what I would try
and maybe it will help you or give you a different way to
look at it.

I'd create a clone of frame4, maybe frame5, only viewing
one at a time.

In my refresh routine I'd create some type of loop
and alternate them using .hide() .show() and only
do my drawing/erasing on the hidden object.

I think anyway you do it, you should only draw/erase
on a hidden object, you could also have frame4 and 5
be the same, but in the routine that calls refresh, .hide()
4, .show() 5, refresh then redraws 4, and on returning from
refresh, .hide() 5, .show()4. Anyway I thing you get the idea.

Just something I'd try.

More information about the Python-list mailing list