tags:

views:

75

answers:

3

Hello,

How to call from class oneThread: back to class fun:? As in, address a class written below. Is it possible?

class oneThread(threading.Thread):
        def __init__(self):
                threading.Thread.__init__(self)
                self.start()

        def run(self):            
            print "1"
            time.sleep(1)
            print "2"
            time.sleep(1)
            print "3"
            self.wTree.get_widget("entryResult").set_text("Done with One.") # How to call from here back to class fun, which of course is below...? 

class fun:
        wTree = None
        def __init__( self ):                
                self.wTree = gtk.glade.XML( "main.glade" )
                self.wTree.signal_autoconnect( {"on_buttonOne" : self.one} )              
                gtk.main()

        def one(self, widget):
                oneThread();

gtk.gdk.threads_init()         
do=fun()
A: 

You would need to pass the fun instance to the oneThread constructor:

class oneThread(threading.Thread):
    def __init__(self, fun):
        self.fun = fun
        ...

class fun:
    def one(self, widget):
        oneThread(self):
Michael Mrozek
or just pass wTree, if that's all you care about.
dash-tom-bang
I get an error: AttributeError: 'oneThread' object has no attribute 'wTree'@dash-tom-bang can you give me an example please.
rename all instances of 'fun' in the above example to 'wTree'.
dash-tom-bang
Not working. AttributeError: fun instance has no attribute 'get_widget'. What could be wrong...
+1  A: 

You need to use the right references to the full object or just a field of (the wTree).

self.fun.wTree if you pass self to the oneThread class

self.wTree if you pass the gtk.glade.XML Object

see the comments...

class oneThread(threading.Thread):
    def __init__(self, reference):
        self.fun = reference
        #or self.wTree = reference
        threading.Thread.__init__(self)
        self.start()

    def run(self):
        print "1"
        time.sleep(1)
        print "2"
        time.sleep(1)
        print "3"
        self.fun.wTree.get_widget("entryResult").set_text("Done with One.")
        # or self.wTree.get_widget("entryResult").set_text("Done with One.")

class fun(object):
    def __init__( self ):
        self.wTree = gtk.glade.XML( "main.glade" )
        self.wTree.signal_autoconnect( {"on_buttonOne" : self.one} )
        gtk.main()

    def one(self, widget):
        oneThread(self)
        # or oneThread(self.wTree)
cyraxjoe
+1  A: 

The elegant solution would be to pass the reference to class fun instance to the oneThread constructor, as other suggested, but you seem to be trying to do something else: making the fun class as singleton an accessing it as a global object. Is that right? That is usually a bad idea, but sometimes makes sense.

If that is the case, then you are making two mistakes:

  1. you use self.fun in oneThread.run, although 'fun' is not part of the oneThread class or its instance. You should be using just 'fun' to access the class 'fun'. It doesn't matter it is defined below, as the code will be executed when the class is already defined.
  2. in fun.__init__ you are not writting the class attribute fun.wTree, but creating a instance attribute wTree. The class attribute fun.wTree will stay None. To change it use fun.wTree = gtk.glade.XML( "main.glade" ) instead (though, you may keep using self.wTree to access it later).

So the code would look like this:

class oneThread(threading.Thread):
    def __init__(self):
            threading.Thread.__init__(self)
            self.start()

    def run(self):            
        print "1"
        time.sleep(1)
        print "2"
        time.sleep(1)
        print "3"
        fun.wTree.get_widget("entryResult").set_text("Done with One.") 

class fun:
    wTree = None
    def __init__( self ):                
            fun.wTree = gtk.glade.XML( "main.glade" )
            self.wTree.signal_autoconnect( {"on_buttonOne" : self.one} )              
            gtk.main()

    def one(self, widget):
            oneThread();

gtk.gdk.threads_init()         
do=fun()

I would also suggest renaming wTree attribute to instance in such case too.

And once more: this (using a singleton as a kind of global variable) is probably not the way to go.

Jacek Konieczny