简体   繁体   English

是否只有gobject.idle_add()函数终止时才能继续?

[英]Is there a way to continue only when gobject.idle_add() function terminate?

I want to download images and show the progress with a gtk.ProgressBar() . 我想下载图像并使用gtk.ProgressBar()显示进度。

For this, I used the method recommended in the PyGTK FAQ and it works fine : 为此,我使用了PyGTK常见问题解答中推荐的方法,并且效果很好:

class MainInterface :
  def __init__(self) :
    ...
    thread.start_new_thread(self.getMap, ())

    gtk.gdk.threads_init()
    gtk.gdk.threads_enter()
    gtk.main()
    gtk.gdk.threads_leave()

  def getMap(self) :
    ...
    getImage = self.getImages(new_img_list) # new_img_list is a list of image paths
    gobject.idle_add(getImage.next)

  def getImages(self, img_list) :
    nb_img = len(img_list)
    i = 1.0
    for img in img_list :
        self.progress_interface.progress_bar.set_fraction(i/nb_img)
        gtk.gdk.threads_enter()
        get_maps.download_image(img)    # This function downloads the current image to the disk
        gtk.gdk.threads_leave()
        i += 1.0
        yield True
    self.progress_interface.progress_bar.set_text("Les images sont à jour")
    self.progress_interface.progress_bar.set_fraction(1)
    yield False

Now, I have a ShowMap() method which create gtk.Pixbuf from the images previously downloaded. 现在,我有一个ShowMap()方法,该方法可以从以前下载的图像创建gtk.Pixbuf

So, I want to call this ShowMap() function when all the images in new_img_list are downloaded (so when getImage.next() returns Falsè ). 因此,当要下载new_img_list中的所有图像时(因此,当getImage.next()返回Falsè ),我想调用此ShowMap()函数。

How I can to do this ? 我该怎么做?

Thanks 谢谢

I've found a solution : start ShowMap() in a new thread in getImages() , like this : 我找到了一个解决方案:在getImages()的新线程中启动ShowMap() getImages() ,如下所示:

def getImages(self, img_list) :
    nb_img = len(img_list)
    i = 1.0
    for img in img_list :
        self.progress_interface.set_progress(i, nb_img)
        gtk.gdk.threads_enter()
        get_maps.download_image(img)
        gtk.gdk.threads_leave()
        i += 1.0
        yield True
    self.progress_interface.progress_bar.set_text("Les images sont à jour")
    self.progress_interface.progress_bar.set_fraction(1)
    thread.start_new_thread(self.ShowMap, ()) # Here
    yield False

But maybe it have a better solution ? 但是,也许有更好的解决方案?

EDIT : In fact, this solution doesn't works on Windows (but works on Linux), so it's probably not a proper way to do this. 编辑:实际上,此解决方案不适用于Windows(但适用于Linux),因此它可能不是正确的方法。

EDIT 2 : I finally found the solution by also using a generator function to progressively add rows in the gtk.IconView and passing it to gobject.idle_add() : 编辑2:我终于找到了解决方案,方法是同时使用生成器函数在gtk.IconView逐步添加行并将其传递给gobject.idle_add()

def getImages(self, img_list) :
    nb_img = len(img_list)
    i = 1.0
    for img in img_list :
        self.progress_interface.set_progress(i, nb_img)
        gtk.gdk.threads_enter()
        get_maps.download_image(img)
        gtk.gdk.threads_leave()
        i += 1.0
        yield True
    self.progress_interface.progress_bar.set_text("Les images sont à jour")
    self.progress_interface.progress_bar.set_fraction(1)
    map_loading = self.loadMap()
    gobject.idle_add(map_loading.next)
    yield False

def loadMap(self, step=128) :
    i = 1.0
    nb_tiles = len(self.graph) * len(self.graph[0])
    self.grid.iconview.freeze_child_notify()
    for row in self.graph :
        for col in row :
            ...
            self.grid.listStore.append(...)
            self.progress_interface.progressbar_map.set_fraction(i/nb_tiles)
            self.progress_interface.progressbar_map.set_text("Chargement des tuiles en cours ...")
            i += 1.0
            if (i % step) == 0:
                self.grid.iconview.thaw_child_notify()
                yield True
                self.grid.iconview.freeze_child_notify()
    self.grid.iconview.set_model(self.grid.listStore)
    self.grid.iconview.thaw_child_notify()
    yield False

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM