Skip to content
This repository

Gtk3 integration with ipython works. #1588

Merged
merged 7 commits into from about 2 years ago

3 participants

Thomi Richards Fernando Perez Thomas Kluyver
Thomi Richards

This branch adds support for interactive Gtk3 UI support.

The older Gtk2 bindings (pygtk) are being deprecated in the wild, in favor of the newer gi bindings. This allows users to use new features in Gtk3. This is a very simple branch, that follows the same pattern as the 'gtk' UI support feature, but for Gtk3.

added some commits April 13, 2012
Fernando Perez
Owner

Do you have any tests we can use to see this functionality at work? It could be an interactive example that goes into docs/examples/lib if it's hard to unittest this...

Also, since this adds a 'gtk3' target for the %gui magic, the docstring for magic_gui needs to be updated to show that gtk3 is also a valid value.

Thomi Richards

Hi,

Thanks for looking at this. Both changes have been made now. I added a docstring to the %gui magic function. I notice that the UI types weren't listed alphabetically, so I put the docstring for 'gtk3' right below the docstring or 'gtk'. As far as testing is concerned, I've followed the pattern for the 'gtk' Gui integration.

IPython/core/magic.py
... ...
@@ -94,7 +94,7 @@ def needs_local_scope(func):
94 94
     func.needs_local_scope = True
95 95
     return func
96 96
 
97  
-    
  97
+
1
Thomas Kluyver Collaborator
takluyver added a note April 14, 2012

For future reference: please try to avoid whitespace changes in functional pull requests, because it makes it hard to see what's really changed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
docs/examples/lib/gui-gtk3.py
... ...
@@ -0,0 +1,37 @@
  1
+#!/usr/bin/env python
  2
+"""Simple Gtk example to manually test event loop integration.
  3
+
  4
+This is meant to run tests manually in ipython as:
  5
+
  6
+In [1]: %gui gtk3
  7
+
  8
+In [2]: %run gui-gtk3.py
  9
+"""
  10
+
  11
+from gi.repository import Gtk
  12
+
  13
+
  14
+def hello_world(wigdet, data=None):
  15
+    print "Hello World"
1
Thomas Kluyver Collaborator
takluyver added a note April 14, 2012

Can you change this to print("Hello World") so it's easy to test from Python 3 (I've just done it locally, and it does work). The brackets are valid in Python 2 for a single argument.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Fernando Perez
Owner

@thomir, it seems that (in addition to the small fix suggested by @takluyver), this PR now has a conflict with master, likelye something small that's colliding with other recently merged code. We'll have to ask you to rebase it before we can merge it, unfortunately.

Let us know if you need help with that and we'll give you a hand. Once you rebase and make that small fix I'll take care of merging it quickly so this doesn't happen again.

Thomas Kluyver
Collaborator

It may well be the whitespace changes conflicting with a change in nearby lines.

Fernando Perez
Owner

Yes, very likely, though I haven't confirmed yet by looking at the conflicts.

Thomi Richards

Hi,

Sorry about the whitespace changes - my editor automatically strips trailing whitespace, so I didn't even notice I had done this.

I've merged trunk, and fixed the conflicts. I think I've done this correctly, although I'm using the git backend to bzr, so there's a chance it's not correct.

Thanks again for looking at this.

IPython/core/magic.py
((13 lines not shown))
2263 2272
             contents = openpy.read_py_file(arg_s, skip_encoding_cookie=True)
2264 2273
         
2265 2274
         self.set_next_input(contents)
  2275
+>>>>>>> MERGE-SOURCE
1
Thomas Kluyver Collaborator
takluyver added a note April 14, 2012

Nope, this merge didn't quite go right.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Thomas Kluyver
Collaborator

The docstring change for the %gui magic seems to have gone missing after the latest merge.

Thomi Richards

OK, I figured out how to use rebase properly :)

Thomi Richards

whitespace-only changes reverted.

Thomas Kluyver takluyver merged commit 9096723 into from April 14, 2012
Thomas Kluyver takluyver closed this April 14, 2012
Thomas Kluyver
Collaborator

@fperez already OKed the changes, so I've merged this.

Fernando Perez
Owner

Many thanks to all!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
1  IPython/core/magic.py
@@ -3420,6 +3420,7 @@ def magic_gui(self, parameter_s=''):
3420 3420
             %gui wx      # enable wxPython event loop integration
3421 3421
             %gui qt4|qt  # enable PyQt4 event loop integration
3422 3422
             %gui gtk     # enable PyGTK event loop integration
  3423
+            %gui gtk3    # enable Gtk3 event loop integration
3423 3424
             %gui tk      # enable Tk event loop integration
3424 3425
             %gui OSX     # enable Cocoa event loop integration
3425 3426
                          # (requires %matplotlib 1.1) 
1  IPython/lib/__init__.py
@@ -21,6 +21,7 @@
21 21
     enable_tk, disable_tk,
22 22
     enable_glut, disable_glut,
23 23
     enable_pyglet, disable_pyglet,
  24
+    enable_gtk3, disable_gtk3,
24 25
     set_inputhook, clear_inputhook,
25 26
     current_gui
26 27
 )
31  IPython/lib/inputhook.py
@@ -36,6 +36,7 @@
36 36
 GUI_OSX = 'osx'
37 37
 GUI_GLUT = 'glut'
38 38
 GUI_PYGLET = 'pyglet'
  39
+GUI_GTK3 = 'gtk3'
39 40
 GUI_NONE = 'none' # i.e. disable
40 41
 
41 42
 #-----------------------------------------------------------------------------
@@ -421,6 +422,33 @@ def disable_pyglet(self):
421 422
         """
422 423
         self.clear_inputhook()
423 424
 
  425
+    def enable_gtk3(self, app=None):
  426
+        """Enable event loop integration with Gtk3 (gir bindings).
  427
+
  428
+        Parameters
  429
+        ----------
  430
+        app : ignored
  431
+           Ignored, it's only a placeholder to keep the call signature of all
  432
+           gui activation methods consistent, which simplifies the logic of
  433
+           supporting magics.
  434
+
  435
+        Notes
  436
+        -----
  437
+        This methods sets the PyOS_InputHook for Gtk3, which allows
  438
+        the Gtk3 to integrate with terminal based applications like
  439
+        IPython.
  440
+        """
  441
+        from IPython.lib.inputhookgtk3 import inputhook_gtk3
  442
+        self.set_inputhook(inputhook_gtk3)
  443
+        self._current_gui = GUI_GTK
  444
+
  445
+    def disable_gtk3(self):
  446
+        """Disable event loop integration with PyGTK.
  447
+
  448
+        This merely sets PyOS_InputHook to NULL.
  449
+        """
  450
+        self.clear_inputhook()
  451
+
424 452
     def current_gui(self):
425 453
         """Return a string indicating the currently active GUI or None."""
426 454
         return self._current_gui
@@ -439,6 +467,8 @@ def current_gui(self):
439 467
 disable_glut = inputhook_manager.disable_glut
440 468
 enable_pyglet = inputhook_manager.enable_pyglet
441 469
 disable_pyglet = inputhook_manager.disable_pyglet
  470
+enable_gtk3 = inputhook_manager.enable_gtk3
  471
+disable_gtk3 = inputhook_manager.disable_gtk3
442 472
 clear_inputhook = inputhook_manager.clear_inputhook
443 473
 set_inputhook = inputhook_manager.set_inputhook
444 474
 current_gui = inputhook_manager.current_gui
@@ -480,6 +510,7 @@ def enable_gui(gui=None, app=None):
480 510
             GUI_QT4: enable_qt4,
481 511
             GUI_GLUT: enable_glut,
482 512
             GUI_PYGLET: enable_pyglet,
  513
+            GUI_GTK3: enable_gtk3,
483 514
             }
484 515
     try:
485 516
         gui_hook = guis[gui]
34  IPython/lib/inputhookgtk3.py
... ...
@@ -0,0 +1,34 @@
  1
+# encoding: utf-8
  2
+"""
  3
+Enable Gtk3 to be used interacive by IPython.
  4
+
  5
+Authors: Thomi Richards
  6
+"""
  7
+#-----------------------------------------------------------------------------
  8
+# Copyright (c) 2012, the IPython Development Team.
  9
+#
  10
+# Distributed under the terms of the Modified BSD License.
  11
+#
  12
+# The full license is in the file COPYING.txt, distributed with this software.
  13
+#-----------------------------------------------------------------------------
  14
+
  15
+#-----------------------------------------------------------------------------
  16
+# Imports
  17
+#-----------------------------------------------------------------------------
  18
+
  19
+import sys
  20
+from gi.repository import Gtk, GLib
  21
+
  22
+#-----------------------------------------------------------------------------
  23
+# Code
  24
+#-----------------------------------------------------------------------------
  25
+
  26
+def _main_quit(*args, **kwargs):
  27
+    Gtk.main_quit()
  28
+    return False
  29
+
  30
+
  31
+def inputhook_gtk3():
  32
+    GLib.io_add_watch(sys.stdin, GLib.IO_IN, _main_quit)
  33
+    Gtk.main()
  34
+    return 0
37  docs/examples/lib/gui-gtk3.py
... ...
@@ -0,0 +1,37 @@
  1
+#!/usr/bin/env python
  2
+"""Simple Gtk example to manually test event loop integration.
  3
+
  4
+This is meant to run tests manually in ipython as:
  5
+
  6
+In [1]: %gui gtk3
  7
+
  8
+In [2]: %run gui-gtk3.py
  9
+"""
  10
+
  11
+from gi.repository import Gtk
  12
+
  13
+
  14
+def hello_world(wigdet, data=None):
  15
+    print("Hello World")
  16
+
  17
+def delete_event(widget, event, data=None):
  18
+    return False
  19
+
  20
+def destroy(widget, data=None):
  21
+    Gtk.main_quit()
  22
+
  23
+window = Gtk.Window(Gtk.WindowType.TOPLEVEL)
  24
+window.connect("delete_event", delete_event)
  25
+window.connect("destroy", destroy)
  26
+button = Gtk.Button("Hello World")
  27
+button.connect("clicked", hello_world, None)
  28
+
  29
+window.add(button)
  30
+button.show()
  31
+window.show()
  32
+
  33
+try:
  34
+    from IPython.lib.inputhook import enable_gtk3
  35
+    enable_gtk3()
  36
+except ImportError:
  37
+    Gtk.main()
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.