Diez B. Roggisch wrote:
Maybe it helps to put your sleep not in the drawing code, but between the
arrival of different data sets to draw, thus limiting the redraws to an
amount of lets say 10/s.
Yes - tried that, but gives incomplete draw / flicker.
If you'd fill us in with some more details, the suggestions might be
better. And I think it would be worth asking these qustions on the PyKDE
list Jim has mentioned - I'm also there :)
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.
Also, how can I write the counters to the form? It uses the LCD widget now,
but there must be another way.
TIA
Peter
-- sort1.py ---
#!/usr/bin/python
#
# demo of sort algorithms, using PyQt
import sys, random, time
from qt import *
from frmtest import frmTest
class dlgForm(frmTest ):
def __init__(self, parent=None):
frmTest.__init_ _(self, parent)
self.connect(se lf.cmdQuit, SIGNAL("clicked ()"),self,
SLOT("close()") )
self.connect(se lf.cmd2, SIGNAL("clicked ()"), self.reshuffle)
self.connect(se lf.cmdSort, SIGNAL("clicked ()"), self.sort1)
self.connect(se lf.cmdBubble, SIGNAL("clicked ()"), self.bubble)
self.connect(se lf.cmdRipple, SIGNAL("clicked ()"), self.ripple)
self.connect(se lf.cmdShell, SIGNAL("clicked ()"), self.shells)
self.refresh()
def reshuffle(self) :
global cComp, cSwap, b1, b2, r1, r2
cComp, cSwap, b1, b2 = [0,0,0,len(l1)]
random.shuffle( l1)
self.refresh()
def sort1(self):
global cComp, cSwap, b1, b2, r1, r2
cComp, cSwap, b1, b2 = [0,0,0,len(l1)]
l1.sort()
self.refresh()
def bubble(self): # simple bubble sort
global cComp, cSwap, b1, b2, r1, r2
cComp, cSwap, b1, b2 = [0,0,0,len(l1)]
b1 = 0
b2 = len(l1)
for i in range(len(l1)):
b2 = len(l1) - i
for j in range(b2-1):
cComp = cComp + 1
r1 = j
r2 = j+1
if l1[r1] > l1[r2]:
cSwap = cSwap + 1
l1[r1], l1[r2] = [l1[r2], l1[r1]]
self.refresh()
time.sleep(0.02 )
def ripple(self):
global cComp, cSwap, b1, b2, r1, r2
cComp, cSwap, b1, b2 = [0,0,0,len(l1)]
bDone =1
while bDone:
bDone = 0
for j in range(b1,b2-1):
cComp = cComp + 1
r1 = j
r2 = j + 1
if l1[r1] > l1[r2]:
cSwap = cSwap + 1
bDone = 1
l1[r1], l1[r2] = [l1[r2], l1[r1]]
self.refresh()
time.sleep(0.01 )
b2 = b2 - 1
for j in range(b2,b1,-1):
cComp = cComp + 1
r1 = j - 1
r2 = j
if l1[r1] > l1[r2]:
cSwap = cSwap + 1
bDone = 1
l1[r1], l1[r2] = [l1[r2], l1[r1]]
self.refresh()
time.sleep(0.01 )
b1 = b1 + 1
self.refresh()
def shells(self):
global cComp, cSwap, b1, b2, r1, r2
cComp, cSwap, b1, b2 = [0,0, 0, len(l1)]
bDone = 1
span = 64
while bDone:
bDone = 0
for i in range(b1,b2-span):
r1 = i
r2 = i + span
cComp = cComp + 1
if l1[r1] > l1[r2]:
cSwap = cSwap + 1
bDone = 1
l1[r1], l1[r2] = [l1[r2], l1[r1]]
self.refresh()
time.sleep(0.01 )
if not bDone: # if no changes that sweep, exit
break
bDone = 0
if span > 1:
span = int(span/2)
for i in range(b2-1,b1+span,-1):
r1 = i - span
r2 = i
cComp = cComp + 1
if l1[r1] > l1[r2]:
cSwap = cSwap + 1
bDone = 1
l1[r1], l1[r2] = [l1[r2], l1[r1]]
self.refresh()
time.sleep(0.01 )
if span > 1:
span = int(span/2)
self.refresh()
def refresh(self):
self.LCDcount.d isplay('%3d' % cComp) # update counters
self.LCDswap.di splay('%3d' % cSwap)
self.frame4.era se() # clear any old lines
p = QPainter(self.f rame4)
mar = self.frame4.wid th()/100 # margin
vsp = 2 # vertical spacing
hrz = (self.frame4.wi dth()-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+v sp*b1-1)
p.drawLine(mar, mar+vsp*b2-1,hrz*100,mar+v sp*b2-1)
p.setPen(QColor ("red"))
p.drawLine(mar, mar+vsp*r1+1,hr z*100,mar+vsp*r 1+1)
p.drawLine(mar, mar+vsp*r2+1,hr z*100,mar+vsp*r 2+1)
if __name__ == '__main__':
l1 = range(100) # numbers 0 to 99
b1 = 0 # blue line 1
b2 = 100 # blue line 2
r1 = 1 # red line 1
r2 = 1 # red line 2
cComp = 0 # count of compares
cSwap = 0 # count of swaps
app = QApplication(sy s.argv)
QObject.connect (app, SIGNAL('lastWin dowClosed()'),
app, SLOT('quit()'))
win = dlgForm()
app.setMainWidg et(win)
win.show()
app.exec_loop()
-------------------------------------------------------
-------- frmtest.py --------------------------
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'frmtest.ui'
#
# Created: Sun Aug 1 15:55:11 2004
# by: The PyQt User Interface Compiler (pyuic) 3.8
#
# WARNING! All changes made in this file will be lost!
from qt import *
class frmTest(QMainWi ndow):
def __init__(self,p arent = None,name = None,fl = 0):
QMainWindow.__i nit__(self,pare nt,name,fl)
self.statusBar( )
if not name:
self.setName("f rmTest")
self.setCentral Widget(QWidget( self,"qt_centra l_widget"))
self.frame4 = QFrame(self.cen tralWidget(),"f rame4")
self.frame4.set Geometry(QRect( 40,109,441,210) )
self.frame4.set MinimumSize(QSi ze(440,210))
self.frame4.set FrameShape(QFra me.StyledPanel)
self.frame4.set FrameShadow(QFr ame.Raised)
self.frame4.set Margin(1)
self.cmd2 = QPushButton(sel f.centralWidget (),"cmd2")
self.cmd2.setGe ometry(QRect(21 0,330,100,50))
self.cmdSort = QPushButton(sel f.centralWidget (),"cmdSort")
self.cmdSort.se tGeometry(QRect (350,330,100,50 ))
self.cmdQuit = QPushButton(sel f.centralWidget (),"cmdQuit")
self.cmdQuit.se tGeometry(QRect (70,330,100,50) )
self.txtHello = QLabel(self.cen tralWidget(),"t xtHello")
self.txtHello.s etGeometry(QRec t(50,20,450,76) )
txtHello_font = QFont(self.txtH ello.font())
txtHello_font.s etPointSize(48)
self.txtHello.s etFont(txtHello _font)
self.txtHello.s etAlignment(QLa bel.AlignCenter )
self.cmdBubble = QPushButton(sel f.centralWidget (),"cmdBubble" )
self.cmdBubble. setGeometry(QRe ct(500,109,80,4 0))
self.cmdRipple = QPushButton(sel f.centralWidget (),"cmdRipple" )
self.cmdRipple. setGeometry(QRe ct(500,160,80,4 0))
self.cmdShell = QPushButton(sel f.centralWidget (),"cmdShell")
self.cmdShell.s etGeometry(QRec t(500,210,80,40 ))
self.txtCopyrig ht = QLabel(self.cen tralWidget(),"t xtCopyright")
self.txtCopyrig ht.setGeometry( QRect(40,380,40 0,21))
self.LCDswap = QLCDNumber(self .centralWidget( ),"LCDswap")
self.LCDswap.se tGeometry(QRect (490,370,91,31) )
self.LCDswap.se tSegmentStyle(Q LCDNumber.Flat)
self.LCDcount = QLCDNumber(self .centralWidget( ),"LCDcount")
self.LCDcount.s etGeometry(QRec t(490,329,91,31 ))
self.LCDcount.s etMode(QLCDNumb er.Dec)
self.LCDcount.s etSegmentStyle( QLCDNumber.Flat )
self.languageCh ange()
self.resize(QSi ze(603,438).exp andedTo(self.mi nimumSizeHint() ))
self.clearWStat e(Qt.WState_Pol ished)
def languageChange( self):
self.setCaption (self.__tr("tes t form"))
self.cmd2.setTe xt(self.__tr("R eshuffle"))
self.cmdSort.se tText(self.__tr ("Sort"))
self.cmdQuit.se tText(self.__tr ("Quit"))
self.txtHello.s etText(self.__t r("Sort Demo"))
self.cmdBubble. setText(self.__ tr("Bubble"))
self.cmdRipple. setText(self.__ tr("Ripple"))
self.cmdShell.s etText(self.__t r("Shell"))
self.txtCopyrig ht.setText(self .__tr("Copyrigh t 2004 GPL Peter"))
def __tr(self,s,c = None):
return qApp.translate( "frmTest",s ,c)
---------------------------------------------------------