FAQ Index - Search - Recent Changes - Everything - Add entry

<< Previous Entry | FAQ Entry 20.21 | Next Entry >>

20.21. How do I download something without freezing the GUI (and without threading)?

Using Gio.File.read_async, as the following code, which downloads pygtk's homepage and displays part of its html, shows:

    import gio, gtk

    class Downloader(object):
        def get(self, data, url):
            self.stream = gio.File(url)
            self.stream.read_async(self.read)

        def read(self, gdaemonfile, result):
            label.set_text(self.stream.read_finish(result).read()[:100])
    down = Downloader()

    dial = gtk.Dialog()
    label = gtk.Label()
    dial.action_area.pack_start(label)
    label.show_all()
    label.connect('realize', down.get, "http://www.pygtk.org")
    dial.run()

PyGTK FAQ Wizard | PyGTK Homepage | Feedback to faq at pygtk.org