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, url):
            gfile = gio.File(url)
            gfile.read_async(self._file_read_cb)

        def _file_read_cb(self, gfile, result):
            stream = gfile.read_finish(result)
            # This will only read the first 100 bytes.
            stream.read_async(100, self._stream_read_cb)

        def _stream_read_cb(self, stream, result):
            data = stream.read_finish(result)
            label.set_text(data)

    down = Downloader()

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

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