Most viewed recipes tagged "gtk"http://code.activestate.com/recipes/tags/gtk/views/2013-11-27T14:03:21-08:00ActiveState Code RecipesRun asynchronous tasks using coroutines (Python) 2010-08-06T16:16:20-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577129-run-asynchronous-tasks-using-coroutines/ <p style="color: grey"> Python recipe 577129 by <a href="/recipes/users/4173270/">Arnau Sanchez</a> (<a href="/recipes/tags/coroutine/">coroutine</a>, <a href="/recipes/tags/event/">event</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/gobject/">gobject</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/network/">network</a>, <a href="/recipes/tags/nonblocking/">nonblocking</a>, <a href="/recipes/tags/pygtk/">pygtk</a>). Revision 20. </p> <p>This recipe shows a simple, transparent (and hopefully pythonic) way of running asynchronous tasks when writing a event-driven application (i.e. GUI). The aim is to allow a programmer to write time-consuming functions (usually IO-bound, but not only) with sequential-looking code, instead of scattering the logic over a bunch of callbacks. We will take advantage of the coroutines introduced in Python 2.5 (see <a href="http://www.python.org/dev/peps/pep-0342" rel="nofollow">http://www.python.org/dev/peps/pep-0342</a>). </p> <p>The goal: wouldn't it be great if we could write something like this?</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = function_that_takes_eons_to_complete(arg1, arg2) result2 = another_function_that_downloads_a_big_really_big_file(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start_button___clicked(button, entry): myjob(entry, 1, 2, 3) ... gtk.main() </code></pre> <p>Indeed, but we can't! The GUI will hang until the job is done and the user will be rightfully angry. Coroutines to the rescue: the absolute minimal change we can make to this code is transforming <em>myjob</em> into a coroutine and yield every time we do blocking stuff:</p> <pre class="prettyprint"><code>def myjob(entry, arg1, arg2, arg3): result1 = yield some_task(arg1, arg2) result2 = yield some_other_task(result1, arg3) entry.set_text("The result is: %d" % result2) def on_start__clicked(button, entry): start_job(myjob(entry, 1, 2, 3)) </code></pre> <p><em>some_task</em> and <em>some_other_task</em> are here the asynchronous implementation of the sequential tasks used in the first fragment, and <em>start_job</em> the wrapper around the coroutine. Note that we still have to implement non-blocking versions of the tasks, but they are usually pretty generic (wait some time, download a file, ...) and can be re-used. If you happen to have a CPU-bound function or even a IO-bound code you cannot split (<em>urllib2</em> anyone?), you can always use a generic threaded task (granted, the whole point of using co-routines should be avoiding threads, but there is no alternative here).</p> <p>At the end, all the plumbing we need to make it work is just 1 function: <em>start_job</em> (wrapper around the job to manage the flow of the coroutine). The rest of the code -two asynchronous tasks (<em>sleep_task</em>, <em>threaded_task</em>) and a demo app- are shown solely as an example.</p> Basic Gtk.TreeView Example with two sortable columns (Python) 2013-11-27T13:56:38-08:00Anonimistahttp://code.activestate.com/recipes/users/4188571/http://code.activestate.com/recipes/578772-basic-gtktreeview-example-with-two-sortable-column/ <p style="color: grey"> Python recipe 578772 by <a href="/recipes/users/4188571/">Anonimista</a> (<a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/user_interface/">user_interface</a>). </p> <p>Basic Gtk.TreeView Example with two sortable columns</p> Printing with Python and pyGTK (Python) 2009-06-25T13:54:34-07:00Mark Muzenhardthttp://code.activestate.com/recipes/users/4170846/http://code.activestate.com/recipes/576820-printing-with-python-and-pygtk/ <p style="color: grey"> Python recipe 576820 by <a href="/recipes/users/4170846/">Mark Muzenhardt</a> (<a href="/recipes/tags/drawingarea/">drawingarea</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/pygtk/">pygtk</a>). Revision 2. </p> <p>PyGTK is a very powerful GUI-Toolkit. Nearly everything is well documented, except how to print. I messed around for hours just to solve this problem so I decided to share this cool piece of code!</p> Delete a Gtk.TreeView row (Python) 2013-11-27T13:59:58-08:00Anonimistahttp://code.activestate.com/recipes/users/4188571/http://code.activestate.com/recipes/578773-delete-a-gtktreeview-row/ <p style="color: grey"> Python recipe 578773 by <a href="/recipes/users/4188571/">Anonimista</a> (<a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>). </p> <p>Delete a Gtk.TreeView row</p> Generic flash-videos downloader using webkit (Python) 2010-04-04T11:56:25-07:00Arnau Sanchezhttp://code.activestate.com/recipes/users/4173270/http://code.activestate.com/recipes/577167-generic-flash-videos-downloader-using-webkit/ <p style="color: grey"> Python recipe 577167 by <a href="/recipes/users/4173270/">Arnau Sanchez</a> (<a href="/recipes/tags/download/">download</a>, <a href="/recipes/tags/flash/">flash</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/pygtk/">pygtk</a>, <a href="/recipes/tags/webkit/">webkit</a>). </p> <p>This recipe shows how to build a generic resource logger of webpages using PyGtkWebkit. The demo code implements a generic downloader for flash-video sites.</p> List MySql databases in a Gtk.TreeView (Python) 2013-11-27T14:03:21-08:00Anonimistahttp://code.activestate.com/recipes/users/4188571/http://code.activestate.com/recipes/578774-list-mysql-databases-in-a-gtktreeview/ <p style="color: grey"> Python recipe 578774 by <a href="/recipes/users/4188571/">Anonimista</a> (<a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/gui/">gui</a>, <a href="/recipes/tags/mysqldb/">mysqldb</a>, <a href="/recipes/tags/user_interface/">user_interface</a>). </p> <p>List MySql databases in a Gtk.TreeView</p> GtkWorker (Python) 2007-06-03T09:06:42-07:00Thomas Ahlehttp://code.activestate.com/recipes/users/4060075/http://code.activestate.com/recipes/521881-gtkworker/ <p style="color: grey"> Python recipe 521881 by <a href="/recipes/users/4060075/">Thomas Ahle</a> (<a href="/recipes/tags/blocking/">blocking</a>, <a href="/recipes/tags/glib/">glib</a>, <a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/swingworker/">swingworker</a>, <a href="/recipes/tags/threads/">threads</a>). </p> <p>I got inspired to this class, after having read about the java SwingWorker. The problem is that you sometimes have a tough task you want to do in a GUI program, but you don't want the UI to lock. This recipe solves the problem by running the tough code in a background thread, while still letting you do useful interaction with it, like getting the progress or the latest results.</p> Splash Screen GTK (Python) 2011-10-24T13:34:29-07:00Nicolas Coelhohttp://code.activestate.com/recipes/users/4179691/http://code.activestate.com/recipes/577919-splash-screen-gtk/ <p style="color: grey"> Python recipe 577919 by <a href="/recipes/users/4179691/">Nicolas Coelho</a> (<a href="/recipes/tags/gtk/">gtk</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/screen/">screen</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/splash/">splash</a>). </p> <p>This is a simple example showing how to put a splash screen to work in a GTK app.</p>