473,699 Members | 2,086 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PyQt app in seperate thread

I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )

def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?

Cheers,

Anders

Nov 22 '06 #1
9 12299
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )

def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?
Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil
Nov 22 '06 #2

Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )

def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )

mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?

Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil

OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?

Nov 22 '06 #3
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).
>
I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:
>
----8<--------
>
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
>
class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )
>
def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )
>
>
>
mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'
>
----8<--------
>
The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.
>
There's got to be a way to get this working, right? Can anyone help me
along the right path?
Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil

OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?
No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil
Nov 22 '06 #4

Phil Thompson wrote:
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).

I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:

----8<--------

import sys
from PyQt4 import QtGui
from PyQt4 import QtCore

class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )

def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )



mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'

----8<--------

The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.

There's got to be a way to get this working, right? Can anyone help me
along the right path?
>
Read http://doc.trolltech.com/4.2/threads.html
>
In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.
>
Phil
OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?

No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

A

Nov 22 '06 #5
On 22 Nov 2006 06:43:55 -0800, anders <an************ *@gmail.comwrot e:
>
Phil Thompson wrote:
On Wednesday 22 November 2006 2:06 pm, anders wrote:
Phil Thompson wrote:
On Wednesday 22 November 2006 12:37 pm, anders wrote:
I am writing a plugin for a piece of software in python, and I want to
start up a PyQt GUI in the plugin, without stalling the main thread
while the gui is running (later i will want to pass messages between
the main thread and the gui thread).
>
I'm new to pyqt, so I'm probably doing something very silly, but for
the moment I'm just trying to get the first pyqt tutorial example
running in a seperate thread:
>
----8<--------
>
import sys
from PyQt4 import QtGui
from PyQt4 import QtCore
>
class MyThread( QtCore.QThread ):
def __init__( self ):
QtCore.QThread. __init__( self )
>
def run( self ):
app = QtGui.QApplicat ion( sys.argv )
hello = QtGui.QPushButt on( 'Hello world!' )
hello.resize( 500, 500 )
hello.show()
app.exec_()
QtCore.QThread. terminate( )
>
>
>
mt = MyThread()
mt.start()
print 'Main thread continuing...'
mt.wait()
print 'GUI thread finished.'
>
----8<--------
>
The app starts up (with a warning WARNING: QApplication was not created
in the main() thread. ). I get a window, but no button, and clicking on
the close button does nothing and I have to force the program to quit.
>
There's got to be a way to get this working, right? Can anyone help me
along the right path?

Read http://doc.trolltech.com/4.2/threads.html

In particular the bit that says that exec_() must be called from the main
thread and not from a QThread.

Phil
>
OK so that's all good... so how do I go about doing what I want then?
Can I set up a window in the second thread and start its event loop
without running the event loop in the core app?
No. Read the second sentence of the paragraph I referred to. Also read the
section "QObject Reentrancy".

Phil

OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
It's possible with hackery from C++ but I seriously doubt you can do
it from PyQt.

A

--
http://mail.python.org/mailman/listinfo/python-list
Nov 22 '06 #6
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat, done
so myself.

Diez
Nov 22 '06 #7

Diez B. Roggisch wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat, done
so myself.

Diez
Yeah I was thinking that's going to have to be the way to go... I can't
run the main app in a child thread, so I'll have to spawn the GUI as a
seperate process and communicate with it. Didn't know about pyro
though, thanks for the link. You've used it successfully with PyQt in a
seperate process?

Cheers,

Anders

Nov 22 '06 #8
anders wrote:
>
Diez B. Roggisch wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?

Maybe you can either invert the thread-roles - that is, run your "main"
application in a thread, and if needed start the Qt-thing, or you might
consider spawning a process and using pyro. Which will work very neat,
done so myself.

Diez

Yeah I was thinking that's going to have to be the way to go... I can't
run the main app in a child thread, so I'll have to spawn the GUI as a
seperate process and communicate with it. Didn't know about pyro
though, thanks for the link. You've used it successfully with PyQt in a
seperate process?
Yup. I used it to spawn a python interpreter in a subprocess, and
communicate some stuff between that and my main GUI app. The idea was to
have a scriptable application, which allowed you could kill the script.

Diez
Nov 22 '06 #9
anders wrote:
OK I see that now. Thanks for pointing that out. So basically, I can't
do what I want at all. That's a bit of a pain. Is there no way of
tricking Qt into thinking I'm running it in the main thread?
I have an app which runs Qt in a separate thread and allows the user to send
it python commands from the main thread. Have a look at this code to see
how it works:

http://svn.gna.org/viewcvs/veusz/bra...30&view=markup

Jeremy

--
Jeremy Sanders
http://www.jeremysanders.net/
Nov 23 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3321
by: Serge | last post by:
Hi, I have some intensive code that is running on my main thread. I try to show a status update on a 'status form'. The problem that i have is that because it is running in the same thread the window is not responding to the user. The user is now able to minimize, move the window because the code is too busy on it's own work. (and they are all running on the same thread)
12
5848
by: Brian Keating EI9FXB | last post by:
Hello all, Wonder what approach is used for this problem. I have a MDIApplication, the MDIClinets are to be in a seperate thread. So I've done something like this, // Create a new Show method that starts a new thread public new void Show() { Thread t = new Thread(new ThreadStart(ThreadProc));
4
3364
by: redneon | last post by:
I've set up a seperate thread and put a timer in it but for some reason it's tick event is never fired. This is what I have... .... Thread timerThread = new Thread(new ThreadStart(startTimer)); timerThread.Start(); .... private void startTimer() {
2
2277
by: mwazir | last post by:
Hi all, I have a process thats starts in my application and only terminates when my application is terminated. I want to write the output and the errors of this process to a seperate log file. In order to do this, I spawned two threads. My code looks something like this ' Starting the process oProcessStartInfo = New ProcessStartInfo()
7
10433
by: Joe | last post by:
I need a control to always run in a separate thread from the application. I'm not too sure where to begin with this since the control could be dropped on the form at design time. One possible idea I had was to wrap my control in another control which creates the thread and than creates the control. Any suggestions? -Joe
1
1050
by: Mike TI | last post by:
April 20, 2006 Hi all I want to invoke a job on a seperate thread and then close it on completion. Can anyone point me out to a working example. Thank you. Mike TI
3
34645
by: Martin Pöpping | last post by:
Hello, I want to change a ProgressBar in a separate Thread. My current code is the following: private void changeProgressBarThread() { changeProgressBar(); }
2
3849
by: Jorgen Bodde | last post by:
Hi all, I want to make a small batch copy tool that scans for certain files, and copies them to a specified directory. Since the files are huge (AVI / DIVX) typical 300 to 700 Mb, I want to provide the user with some feedback during the file copy. Here is my dillemma; When I use shutil.move(..,..) I have to wait until it's done, there is no feedback, so the GUI basically hangs. However, shutil.move can be fast because it intelligently...
2
1311
by: binarybrain | last post by:
I have encountered a problem that I am having trouble solving. I have one main UI. From this UI you can bring up a new non-modal Windows form that displays some other data. However, each time a new form is brought up, it is done so in a new thread. What I want to do, is have these new forms (in their new thread) make a call to a public method in my main UI's class. This method(funciton) accepts a string argument (and returns a string as...
0
8697
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9184
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9042
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8929
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8891
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6538
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4634
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3061
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2357
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.