如何从课堂上调用一个线程:回到课堂乐趣:?在中,给下面写的一个类寻址。有可能吗?在
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()
优雅的解决方案是将对类fun实例的引用传递给oneThread构造函数,正如其他人建议的那样,但是您似乎在尝试做其他事情:将fun类作为单例作为全局对象访问它。对吗?这通常是个坏主意,但有时也有道理。在
如果是这样,那么你犯了两个错误:
oneThread.run
中使用self.fun
,尽管'fun'不是oneThread类或其实例的一部分。你应该使用“fun”来访问“fun”类。在下面定义它并不重要,因为代码将在类已经定义后执行。在fun.__init__
中,不是编写类属性fun.wTree
,而是创建一个实例属性wTree
。类属性fun.wTree
将保留None
。去改变它 改用fun.wTree = gtk.glade.XML( "main.glade" )
(不过,以后可以继续使用self.wTree
来访问它)。在所以代码应该是这样的:
在这种情况下,我还建议将
wTree
属性重命名为instance
。在再说一次:这(使用单例变量作为一种全局变量)可能不是正确的方法。在
您需要将
fun
实例传递给oneThread
构造函数:您需要使用对完整对象的正确引用 或者只是一块(树)地。在
self.fun.wTree如果将self传递给oneThread类
self.wTree公司如果您通过gtk.glade.XML对象
查看评论。。。在
相关问题 更多 >
编程相关推荐