views:

340

answers:

2

The basic code that I have so far is below. How do I thread gtk.main() so that the code after Display is initialized runs asynchronously?

import pygtk
pygtk.require("2.0")
import gtk

class Display():

    def __init__(self):
        self.fail = "This will fail to display"
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", lambda w: gtk.main_quit())
        window.show()
        self.main()            

    def main(self):
        gtk.main()

class Test():

    def __init__(self, display):
        print display.fail

d = Display()
t = Test(d)
A: 

Just put the gtk.main call after everything else. If you need to have the controller in a separate thread, make sure you do all gtk related function/methods by doing gobject.idle_add(widget.method).

import pygtk
pygtk.require("2.0")
import gtk

class Display(object):

    def __init__(self):
        self.fail = "This will fail to display"
        window = gtk.Window(gtk.WINDOW_TOPLEVEL)
        window.connect("destroy", lambda w: gtk.main_quit())
        window.show()            


class Test(object):

    def __init__(self, display):
        print display.fail

d = Display()
t = Test(d)

gtk.main()
DoR
A: 

You could use Twisted with the gtk2reactor.

http://twistedmatrix.com/documents/current/core/howto/choosing-reactor.html

Alexandre Quessy
I like the idea, however it is kind of silly to have to import part of or a whole unrelated library to solve this issues. I managed to solve this issue using gobjecct.idle_add() last year.
Toucan