Saturday, May 26, 2012

PyQt GUI FREEZE while loops!

well that because your app has not time to update the gui so you need to use
time.sleep function
 and QtGui.Application.processEvents function....

the time.sleep function takes one arg is an int defined how many seconds the python script will pause...
and the QtGui.Application.processEvents function is a Qt  function that let the app to process the GUI events....
example :
the freezing script:
import itertools ,string
x=1
file=open("file","w")
while x<=200:
 it=itertools.product(string.printable,repeat=x)
for i in it:
ij="".join(i)
 file.write("ij"+"\n")
x=x+1
file.close()
here probably the GUI will freeze because it is a long loop inside another loop...
so here what we can do:

import itertools ,string,time,QtGui
 gui=QtGui.Application.processEvents
x=1
file=open("file","w")
gui()
while x<=200:
 it=itertools.product(string.printable,repeat=x)
time.sleep(0.1)
gui()
for i in it:
ij="".join(i)
 time.sleep(0.001)
gui()
 file.write("ij"+"\n")
gui()
x=x+1
gui()
file.close()
gui()
and now it won't freeze at all :)

3 comments:

  1. Thanks for the usefull trick, using gui=QtGui.Application.processEvents !

    ReplyDelete
  2. so what header did u use ??

    ReplyDelete
    Replies
    1. this is PyQt4 module for python not a header file for C++
      for C++ use QApplication::processEvents()

      Delete

gotta some thing to say ?

Note: Only a member of this blog may post a comment.