PyQt ProgressBar

I’m remaking that little AIR app I posted previously, but doing so in Python. I am using this as my first project to learn python and the QT classes. so far so good. I had a question before I get myself into a corner. I want the thing to be able to open up the command console for pc or terminal for mac for one, any ideas how to have python open a default app on a computer. The other thing(related to the tiltle of the post) is I want to be able to display the progress of your batch render through a progressBar, any clue as to pull that data from the batch render in through the console? I don’t care if it’s complicated or you just tell me the name of the method used and I will figure out the rest.

update: I found the subprocess module and got the Cmd console to pop up, so I guess I just need to know how talk to it

Can’t help you with the pyQT question, but here’s two ways to open a command prompt on the PC:

This blocks until the command prompt is closed:

>>> import os
>>> os.system( ‘cmd’ )

This will not block (unless you put “.wait( )” on the end):

>>> import subprocess
>>> cmd_prompt = subprocess.Popen( ‘cmd’ )

THIS STARTS THE BATCH RENDERING (without blocking just like posted above by Adam :))


proc = subprocess.Popen('c:/program/autodesk/maya2011/bin/render "C:/Users/Yasin/Documents/maya/projects/Immortal/scenes/lighting.mb', 
                        shell=True,
                        stdout=subprocess.PIPE,
                        )

THIS WILL KEEP PRINTING THE OUTPUT ONLY IF IT CONTAINS %, BASICALY RETRIEVES THE OUTPUT :slight_smile:


for i in range(2000):
    output = proc.stdout.readline()
    


    if output.__contains__("%"):
        print output.rstrip()

        #ALSO HANDLE THE % BAR OF THE PYQT UI HERE
        outputSplit = output.split("%")[0]
        lenSplit = len(outputSplit)-1
            
        value = outputSplit[lenSplit-3] + outputSplit[lenSplit-2]
        bar.setValue(int(value))

    if output is "":
        print "RENDER IS DONE!"
        break 

The above code prints this for me, ofc didn’t include it all! :slight_smile:


JOB  0.4  progr:    71.6%    rendered on Yasin-Dator.4
JOB  0.3  progr:    72.0%    rendered on Yasin-Dator.3
JOB  0.4  progr:    72.3%    rendered on Yasin-Dator.4
JOB  0.3  progr:    72.6%    rendered on Yasin-Dator.3
JOB  0.4  progr:    73.0%    rendered on Yasin-Dator.4
JOB  0.3  progr:    73.3%    rendered on Yasin-Dator.3

Now for some PyQT magic, self explanatory I guess :slight_smile:


from PyQt4 import QtCore, QtGui
import subprocess

if __name__ == '__main__':

    import sys

    app = QtGui.QApplication(sys.argv)

    bar = QtGui.QProgressBar()
    bar.setRange(0, 100)
    bar.setValue(50) #SETTINGS THIS TO 50 FOR EXAMPLE
    bar.show()


    #OPEN THE SUB PROCESS HERE
    #............ insert code from above






    #START LOOPING HERE
    #........... insert code from above




    sys.exit(app.exec_())


Tested and it works :slight_smile:

Thanks Adam and LoneWolf, that was a big help. Why you all gotta be so smart? :stuck_out_tongue:

@ LoneWolf, i’m using python 3.1 and its giving me this error when I compile the code

Traceback (most recent call last):
File “C:/Users/Matt/Desktop/progress.pyw”, line 25, in <module>
if output.contains("%"):
TypeError: Type str doesn’t support the buffer API

any ideas? do I need to use an updated syntax?

[QUOTE=mattanimation;9020]
if output.contains("%"):[/QUOTE]
Not sure about syntax update(still using 2.6), But you can try this instead


if '%' in output:

Yeah Akira is right the above one is the correct way to do it :smiley: don’t know why I didn’t think of that one xD //facepalm :slight_smile:

Edit: Found a problem though, when you click something the application kinda goes into not responding mode and the progressbar stops where it was, untill the rendering is done… Not sure what the best way would be to fix this… someone with greater experience might be able to help ya

@Akira -domo arigato goziamasu that seems to work

@LoneWolf - is that for loop with the range 2000 able to be substituted for a type of “listener”(i’ve been using actionScript too much) so that it only runs when there is an output from the console and not just checking 2000 times? Or is that really all you need to do?

It doesn’t loop 2000 times, I just put the 2000 value so it catches every message. What readline does is block execution untill it gets something from the process… I guess you would need something async… I haven’t played much with those type of stuff… Ill see if I have time to dig into it tonight :smiley:

OK so the

if ‘%’ in output

still was throwing the same error so I searched a bit more and found this

if you type it like so

    if (b"%") in output      

or the other way Yasin said it will work. I guess it just needs to see the info as bytes and not a string

LINK:

you can actually cast it as a string and check it. Try this


if '%' in str(output):

This is something to note.

http://www.macaronikazoo.com/?p=607

We ran across this problem in apps that have very verbose output. The most annoying thing is that no errors are actually thrown and the process doesn’t stop. It just sits there and does nothing.

My solution is in the comments.