帮助从ab类调用类

2024-10-04 01:30:02 发布

您现在位置:Python中文网/ 问答频道 /正文

如何从课堂上调用一个线程:回到课堂乐趣:?在中,给下面写的一个类寻址。有可能吗?在

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()

Tags: selfgtktimeinitdefsleepwidgetthread
3条回答

优雅的解决方案是将对类fun实例的引用传递给oneThread构造函数,正如其他人建议的那样,但是您似乎在尝试做其他事情:将fun类作为单例作为全局对象访问它。对吗?这通常是个坏主意,但有时也有道理。在

如果是这样,那么你犯了两个错误:

  1. oneThread.run中使用self.fun,尽管'fun'不是oneThread类或其实例的一部分。你应该使用“fun”来访问“fun”类。在下面定义它并不重要,因为代码将在类已经定义后执行。在
  2. fun.__init__中,不是编写类属性fun.wTree,而是创建一个实例属性wTree。类属性fun.wTree将保留None。去改变它 改用fun.wTree = gtk.glade.XML( "main.glade" )(不过,以后可以继续使用self.wTree来访问它)。在

所以代码应该是这样的:

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()

在这种情况下,我还建议将wTree属性重命名为instance。在

再说一次:这(使用单例变量作为一种全局变量)可能不是正确的方法。在

您需要将fun实例传递给oneThread构造函数:

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

class fun:
    def one(self, widget):
        oneThread(self):

您需要使用对完整对象的正确引用 或者只是一块(树)地。在

self.fun.wTree如果将self传递给oneThread类

self.wTree公司如果您通过gtk.glade.XML对象

查看评论。。。在

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)

相关问题 更多 >