From 5113ef3a8844a20fc204e00f30d7eb10518b6dbc 2009-08-25 19:55:03 From: Brian Granger Date: 2009-08-25 19:55:03 Subject: [PATCH] More work on the Tk and GTK gui integration. --- diff --git a/IPython/__init__.py b/IPython/__init__.py index d1a36c3..632c5fa 100644 --- a/IPython/__init__.py +++ b/IPython/__init__.py @@ -70,7 +70,8 @@ from IPython.lib import ( enable_tk, disable_tk, set_inputhook, clear_inputhook, current_gui, spin, - appstart_qt4, appstart_wx + appstart_qt4, appstart_wx, + appstart_gtk, appstart_tk ) # Release data diff --git a/IPython/lib/__init__.py b/IPython/lib/__init__.py index c0d8767..647be0f 100644 --- a/IPython/lib/__init__.py +++ b/IPython/lib/__init__.py @@ -22,7 +22,8 @@ from IPython.lib.inputhook import ( enable_tk, disable_tk, set_inputhook, clear_inputhook, current_gui, spin, - appstart_qt4, appstart_wx + appstart_qt4, appstart_wx, + appstart_gtk, appstart_tk ) #----------------------------------------------------------------------------- diff --git a/IPython/lib/inputhook.py b/IPython/lib/inputhook.py index 5f052a5..0043379 100755 --- a/IPython/lib/inputhook.py +++ b/IPython/lib/inputhook.py @@ -114,6 +114,56 @@ def appstart_wx(app): app.MainLoop() +def appstart_tk(app): + """Start the tk event loop in a way that plays with IPython. + + When a tk app is run interactively in IPython, the event loop should + not be started. This function checks to see if IPython's tk integration + is activated and if so, it passes. If not, it will call the + :meth:`mainloop` method of the tk object passed to this method. + + This function should be used by users who want their tk scripts to work + both at the command line and in IPython. These users should put the + following logic at the bottom on their script, after they create a + :class:`Tk` instance (called ``app`` here):: + + try: + from IPython.lib.inputhook import appstart_tk + appstart_tk(app) + except ImportError: + app.mainloop() + """ + if app is not None: + if current_gui() == GUI_TK: + pass + else: + app.mainloop() + +def appstart_gtk(): + """Start the gtk event loop in a way that plays with IPython. + + When a gtk app is run interactively in IPython, the event loop should + not be started. This function checks to see if IPython's gtk integration + is activated and if so, it passes. If not, it will call + :func:`gtk.main`. Unlike the other appstart implementations, this does + not take an ``app`` argument. + + This function should be used by users who want their gtk scripts to work + both at the command line and in IPython. These users should put the + following logic at the bottom on their script:: + + try: + from IPython.lib.inputhook import appstart_gtk + appstart_gtk() + except ImportError: + gtk.main() + """ + import gtk + if current_gui() == GUI_GTK: + pass + else: + gtk.main() + #----------------------------------------------------------------------------- # Main InputHookManager class #----------------------------------------------------------------------------- @@ -229,7 +279,12 @@ class InputHookManager(object): This is for internal IPython use only and user code should not call this. Instead, they should issue the raw GUI calls themselves. """ - pass + import gtk + gtk.gdk.threads_enter() + while gtk.events_pending(): + gtk.main_iteration(False) + gtk.gdk.flush() + gtk.gdk.threads_leave() def _spin_tk(self): """Process all pending events in the tk event loop. diff --git a/docs/examples/lib/gui-gtk.py b/docs/examples/lib/gui-gtk.py new file mode 100644 index 0000000..a3eb048 --- /dev/null +++ b/docs/examples/lib/gui-gtk.py @@ -0,0 +1,35 @@ +#!/usr/bin/env python +"""Simple GTK example to manually test event loop integration. + +This is meant to run tests manually in ipython as: + +In [5]: %gui gtk + +In [6]: %run gui-gtk.py +""" + + +import pygtk +pygtk.require('2.0') +import gtk + + +def hello_world(wigdet, data=None): + print "Hello World" + +window = gtk.Window(gtk.WINDOW_TOPLEVEL) +button = gtk.Button("Hello World") +button.connect("clicked", hello_world, None) + +window.add(self.button) +button.show() +window.show() + +try: + from IPython.lib.inputhook import appstart_gtk + appstart_gtk() +except ImportError: + gtk.main() + + + diff --git a/docs/examples/lib/gui-tk.py b/docs/examples/lib/gui-tk.py new file mode 100644 index 0000000..f7366aa --- /dev/null +++ b/docs/examples/lib/gui-tk.py @@ -0,0 +1,32 @@ +#!/usr/bin/env python +"""Simple Tk example to manually test event loop integration. + +This is meant to run tests manually in ipython as: + +In [5]: %gui tk + +In [6]: %run gui-tk.py +""" + +from Tkinter import * + +class MyApp: + + def __init__(self, root): + frame = Frame(root) + frame.pack() + + self.button = Button(frame, text="Hello", command=self.hello_world) + self.button.pack(side=LEFT) + + def hello_world(self): + print "Hello World!" + +root = Tk() + +app = MyApp(root) + +try: + from IPython import appstart_tk; appstart_tk(root) +except ImportError: + root.mainloop()