Skip to content

Commit

Permalink
Merge pull request ipython#1588 from thomir/gtk3-support
Browse files Browse the repository at this point in the history
Add Gtk3 event loop integration and example.
  • Loading branch information
takluyver committed Apr 14, 2012
2 parents 8a0bb92 + 9bbb9ce commit 549ab69
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 0 deletions.
1 change: 1 addition & 0 deletions IPython/core/magic.py
Expand Up @@ -3420,6 +3420,7 @@ def magic_gui(self, parameter_s=''):
%gui wx # enable wxPython event loop integration
%gui qt4|qt # enable PyQt4 event loop integration
%gui gtk # enable PyGTK event loop integration
%gui gtk3 # enable Gtk3 event loop integration
%gui tk # enable Tk event loop integration
%gui OSX # enable Cocoa event loop integration
# (requires %matplotlib 1.1)
Expand Down
1 change: 1 addition & 0 deletions IPython/lib/__init__.py
Expand Up @@ -21,6 +21,7 @@
enable_tk, disable_tk,
enable_glut, disable_glut,
enable_pyglet, disable_pyglet,
enable_gtk3, disable_gtk3,
set_inputhook, clear_inputhook,
current_gui
)
Expand Down
31 changes: 31 additions & 0 deletions IPython/lib/inputhook.py
Expand Up @@ -36,6 +36,7 @@
GUI_OSX = 'osx'
GUI_GLUT = 'glut'
GUI_PYGLET = 'pyglet'
GUI_GTK3 = 'gtk3'
GUI_NONE = 'none' # i.e. disable

#-----------------------------------------------------------------------------
Expand Down Expand Up @@ -421,6 +422,33 @@ def disable_pyglet(self):
"""
self.clear_inputhook()

def enable_gtk3(self, app=None):
"""Enable event loop integration with Gtk3 (gir bindings).
Parameters
----------
app : ignored
Ignored, it's only a placeholder to keep the call signature of all
gui activation methods consistent, which simplifies the logic of
supporting magics.
Notes
-----
This methods sets the PyOS_InputHook for Gtk3, which allows
the Gtk3 to integrate with terminal based applications like
IPython.
"""
from IPython.lib.inputhookgtk3 import inputhook_gtk3
self.set_inputhook(inputhook_gtk3)
self._current_gui = GUI_GTK

def disable_gtk3(self):
"""Disable event loop integration with PyGTK.
This merely sets PyOS_InputHook to NULL.
"""
self.clear_inputhook()

def current_gui(self):
"""Return a string indicating the currently active GUI or None."""
return self._current_gui
Expand All @@ -439,6 +467,8 @@ def current_gui(self):
disable_glut = inputhook_manager.disable_glut
enable_pyglet = inputhook_manager.enable_pyglet
disable_pyglet = inputhook_manager.disable_pyglet
enable_gtk3 = inputhook_manager.enable_gtk3
disable_gtk3 = inputhook_manager.disable_gtk3
clear_inputhook = inputhook_manager.clear_inputhook
set_inputhook = inputhook_manager.set_inputhook
current_gui = inputhook_manager.current_gui
Expand Down Expand Up @@ -480,6 +510,7 @@ def enable_gui(gui=None, app=None):
GUI_QT4: enable_qt4,
GUI_GLUT: enable_glut,
GUI_PYGLET: enable_pyglet,
GUI_GTK3: enable_gtk3,
}
try:
gui_hook = guis[gui]
Expand Down
34 changes: 34 additions & 0 deletions IPython/lib/inputhookgtk3.py
@@ -0,0 +1,34 @@
# encoding: utf-8
"""
Enable Gtk3 to be used interacive by IPython.
Authors: Thomi Richards
"""
#-----------------------------------------------------------------------------
# Copyright (c) 2012, the IPython Development Team.
#
# Distributed under the terms of the Modified BSD License.
#
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------

#-----------------------------------------------------------------------------
# Imports
#-----------------------------------------------------------------------------

import sys
from gi.repository import Gtk, GLib

#-----------------------------------------------------------------------------
# Code
#-----------------------------------------------------------------------------

def _main_quit(*args, **kwargs):
Gtk.main_quit()
return False


def inputhook_gtk3():
GLib.io_add_watch(sys.stdin, GLib.IO_IN, _main_quit)
Gtk.main()
return 0
37 changes: 37 additions & 0 deletions docs/examples/lib/gui-gtk3.py
@@ -0,0 +1,37 @@
#!/usr/bin/env python
"""Simple Gtk example to manually test event loop integration.
This is meant to run tests manually in ipython as:
In [1]: %gui gtk3
In [2]: %run gui-gtk3.py
"""

from gi.repository import Gtk


def hello_world(wigdet, data=None):
print("Hello World")

def delete_event(widget, event, data=None):
return False

def destroy(widget, data=None):
Gtk.main_quit()

window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
window.connect("delete_event", delete_event)
window.connect("destroy", destroy)
button = Gtk.Button("Hello World")
button.connect("clicked", hello_world, None)

window.add(button)
button.show()
window.show()

try:
from IPython.lib.inputhook import enable_gtk3
enable_gtk3()
except ImportError:
Gtk.main()

0 comments on commit 549ab69

Please sign in to comment.