[PyKDE] Starting QApplication from a python thread

Gerard Vermeulen gerard.vermeulen at grenoble.cnrs.fr
Sun Feb 6 07:20:11 GMT 2005


On Sat, 5 Feb 2005 22:16:35 +0000 (GMT)
Jeremy Sanders <jeremy at jeremysanders.net> wrote:

> On a related not to my last posting, why doesn't the following code work?
> 
> -----
> #!/usr/bin/env python
> 
> import thread
> import sys
> import time
> 
> def threadfn():
>      import qt
>      app = qt.QApplication([sys.argv[0]])
>      win = qt.QMainWindow()
>      win.setCaption('foo bar')
>      win.show()
>      app.exec_loop()
> 
> t = thread.start_new_thread(threadfn, ())
> 
> i = 0
> while 1:
>      print i
>      time.sleep(1)
>      i += 1
> 
> ----
> 
> After carefully reading the PyQt docs, it suggests that as long as I keep 
> all pyqt references to a single thread, things should work.
> 
> However the window never appears here, and the infinite loops stops 
> counting.
> 
> Am I missing something obvious.
> 
Read the chapter "Thread Support in Qt" in the Qt-docs. Especially the section
"Thread-safe Event Posting"

You have two solutions for your problem:
(1) PyQwt contains a C++ extension module 'iqt', see
    http://pyqwt.sourceforge.net/doc/iqt-intro.html
    (works only on systems with a GNU readline library, meaning not on Windows)
(2) http://www.boddie.org.uk/david/Projects/Python/KDE/Software/qpython-0.55.tar.gz

The code in (1) is tiny (less than 50 lines), very solid, and orders of magnitude simpler
than the code in (2).  Other toolkits (wxPython for instance) offer only solution (2).
It takes better programmers than me years before they get it bug-free.

Gerard




Gerard
 




More information about the PyQt mailing list