[PyKDE] My thinking still wrong?

Andreas Pakulat apaku at gmx.de
Sat Feb 25 12:14:18 GMT 2006


On 25.02.06 09:55:18, Tina Isaksen wrote:
> def doUpdateSources(self):
>        self.sourcesMessage.setEnabled(1)
>        self.sourcesMessage.setText("Updating your sources, please wait...")
>        os.chdir("/etc/apt/")
>        self.mainTextWindow.setEnabled(1)
>        self.mainTextWindow.setText(commands.getoutput("apt-get update"))
>        self.mainTextWindow.append("Done!")
>        self.mainTextWindow.scrollToBottom()
>        self.pbSaveMain.setEnabled(0)
>        self.mainTextWindow.setReadOnly(1)
> 
> This works but not as I expect it to work. I thought this would first display 
> the the text in 'sourceMessage' before moving on line by line (changing 
> directory, do the 'apt-get update' etc).

No, setText only updates an internal variable of sourcesMessage, the
drawing is done in the next iteration of the event-loop. But this
happens only after your function has finished it's work.

> So I guess I've fallen in the trap of old-time thinking again...

This time it's actually not python-specific "wrong-thinking" ;-) This
would've happend to you in C++, Java and any other threaded-language
with a threaded-toolkit like Qt. You now have 2 Options:

1) Call QApplication::processEvents (IIRC that was the name of the
function) after setting the text. This makes Qt process any pending
events (like the redraw of the label) and then returning to your
code-part, AFAIK.

2) put the "work" into a separate Thread (i.e. the hole
commands.getoutput which takes time) and communicate between the
mainTextWindow and the new Thread via Event's. But befor doing so (and
that is why I'm so brief here) you need to read some basics about
threading, so you don't shoot yourself in the foot with it (for example
never trigger drawing-operations outside the gui-thread). I don't have
any book title or tutorial or some such at hand to recommend, sorry.

> So what I want it to do is to display the 'please wait' in the sourceMessage 
> *before* doing the 'commands.output', just as this little console script does:

This works, because print does output the text directly, without the
need of a redraw-event processed and thus it works.

Andreas

-- 
You feel a whole lot more like you do now than you did when you used to.




More information about the PyQt mailing list