Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 72a79cb

Browse files
committed
Add input hooks for GTK4.
1 parent f50981d commit 72a79cb

11 files changed

Lines changed: 118 additions & 6 deletions

File tree

IPython/core/magics/basic.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,7 @@ def gui(self, parameter_s=''):
493493
%gui qt5 # enable PyQt5 event loop integration
494494
%gui gtk # enable PyGTK event loop integration
495495
%gui gtk3 # enable Gtk3 event loop integration
496+
%gui gtk4 # enable Gtk4 event loop integration
496497
%gui tk # enable Tk event loop integration
497498
%gui osx # enable Cocoa event loop integration
498499
# (requires %matplotlib 1.1)

IPython/core/magics/pylab.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ def matplotlib(self, line=''):
8888
You can list the available backends using the -l/--list option::
8989
9090
In [4]: %matplotlib --list
91-
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'notebook', 'wx', 'qt', 'nbagg',
91+
Available matplotlib backends: ['osx', 'qt4', 'qt5', 'gtk3', 'gtk4', 'notebook', 'wx', 'qt', 'nbagg',
9292
'gtk', 'tk', 'inline']
9393
"""
9494
args = magic_arguments.parse_argstring(self.matplotlib, line)

IPython/core/pylabtools.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
"tk": "TkAgg",
1717
"gtk": "GTKAgg",
1818
"gtk3": "GTK3Agg",
19+
"gtk4": "GTK4Agg",
1920
"wx": "WXAgg",
2021
"qt4": "Qt4Agg",
2122
"qt5": "Qt5Agg",
@@ -44,6 +45,7 @@
4445
# map to the same GUI support
4546
backend2gui['GTK'] = backend2gui['GTKCairo'] = 'gtk'
4647
backend2gui['GTK3Cairo'] = 'gtk3'
48+
backend2gui['GTK4Cairo'] = 'gtk4'
4749
backend2gui['WX'] = 'wx'
4850
backend2gui['CocoaAgg'] = 'osx'
4951
# And some backends that don't need GUI integration

IPython/lib/inputhookgtk4.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
"""
2+
Enable Gtk4 to be used interactively by IPython.
3+
"""
4+
#-----------------------------------------------------------------------------
5+
# Copyright (c) 2021, the IPython Development Team.
6+
#
7+
# Distributed under the terms of the Modified BSD License.
8+
#
9+
# The full license is in the file COPYING.txt, distributed with this software.
10+
#-----------------------------------------------------------------------------
11+
12+
#-----------------------------------------------------------------------------
13+
# Imports
14+
#-----------------------------------------------------------------------------
15+
16+
import sys
17+
18+
from gi.repository import GLib
19+
20+
#-----------------------------------------------------------------------------
21+
# Code
22+
#-----------------------------------------------------------------------------
23+
24+
class _InputHook:
25+
def __init__(self, context):
26+
self._quit = False
27+
GLib.io_add_watch(sys.stdin, GLib.PRIORITY_DEFAULT, GLib.IO_IN, self.quit)
28+
29+
def quit(self, *args, **kwargs):
30+
self._quit = True
31+
return False
32+
33+
def run(self):
34+
context = GLib.MainContext.default()
35+
while not self._quit:
36+
context.iteration(True)
37+
38+
39+
def inputhook_gtk4():
40+
hook = _InputHook()
41+
hook.run()
42+
return 0

IPython/terminal/pt_inputhooks/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"gtk",
1515
"gtk2",
1616
"gtk3",
17+
"gtk4",
1718
"tk",
1819
"wx",
1920
"pyglet",
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
"""
2+
prompt_toolkit input hook for GTK 4.
3+
"""
4+
5+
from gi.repository import GLib
6+
7+
8+
class _InputHook:
9+
def __init__(self, context):
10+
self._quit = False
11+
GLib.io_add_watch(context.fileno(), GLib.PRIORITY_DEFAULT, GLib.IO_IN, self.quit)
12+
13+
def quit(self, *args, **kwargs):
14+
self._quit = True
15+
return False
16+
17+
def run(self):
18+
context = GLib.MainContext.default()
19+
while not self._quit:
20+
context.iteration(True)
21+
22+
23+
def inputhook(context):
24+
hook = _InputHook(context)
25+
hook.run()

docs/source/config/eventloops.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ loop, so you can use both a GUI and an interactive prompt together. IPython
77
supports a number of common GUI toolkits, but from IPython 3.0, it is possible
88
to integrate other event loops without modifying IPython itself.
99

10-
Supported event loops include ``qt4``, ``qt5``, ``gtk2``, ``gtk3``, ``wx``,
11-
``osx`` and ``tk``. Make sure the event loop you specify matches the GUI
12-
toolkit used by your own code.
10+
Supported event loops include ``qt4``, ``qt5``, ``gtk2``, ``gtk3``, ``gtk4``,
11+
``wx``, ``osx`` and ``tk``. Make sure the event loop you specify matches the
12+
GUI toolkit used by your own code.
1313

1414
To make IPython GUI event loop integration occur automatically at every
1515
startup, set the ``c.InteractiveShellApp.gui`` configuration key in your

docs/source/interactive/reference.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ the command-line by passing the full class name and a corresponding value; type
4444
<...snip...>
4545
--matplotlib=<CaselessStrEnum> (InteractiveShellApp.matplotlib)
4646
Default: None
47-
Choices: ['auto', 'gtk', 'gtk3', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
47+
Choices: ['auto', 'gtk', 'gtk3', 'gtk4', 'inline', 'nbagg', 'notebook', 'osx', 'qt', 'qt4', 'qt5', 'tk', 'wx']
4848
Configure matplotlib for interactive use with the default matplotlib
4949
backend.
5050
<...snip...>
@@ -902,7 +902,8 @@ For users, enabling GUI event loop integration is simple. You simple use the
902902
%gui [GUINAME]
903903

904904
With no arguments, ``%gui`` removes all GUI support. Valid ``GUINAME``
905-
arguments include ``wx``, ``qt``, ``qt5``, ``gtk``, ``gtk3`` and ``tk``.
905+
arguments include ``wx``, ``qt``, ``qt5``, ``gtk``, ``gtk3`` ``gtk4``, and
906+
``tk``.
906907

907908
Thus, to use wxPython interactively and create a running :class:`wx.App`
908909
object, do::

examples/IPython Kernel/Index.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@
150150
"&nbsp;&nbsp;<a href='gui/gui-glut.py' target='_blank'>gui-glut.py</a><br>\n",
151151
"&nbsp;&nbsp;<a href='gui/gui-gtk.py' target='_blank'>gui-gtk.py</a><br>\n",
152152
"&nbsp;&nbsp;<a href='gui/gui-gtk3.py' target='_blank'>gui-gtk3.py</a><br>\n",
153+
"&nbsp;&nbsp;<a href='gui/gui-gtk4.py' target='_blank'>gui-gtk4.py</a><br>\n",
153154
"&nbsp;&nbsp;<a href='gui/gui-pyglet.py' target='_blank'>gui-pyglet.py</a><br>\n",
154155
"&nbsp;&nbsp;<a href='gui/gui-qt.py' target='_blank'>gui-qt.py</a><br>\n",
155156
"&nbsp;&nbsp;<a href='gui/gui-tk.py' target='_blank'>gui-tk.py</a><br>\n",
@@ -160,6 +161,7 @@
160161
" gui-glut.py\n",
161162
" gui-gtk.py\n",
162163
" gui-gtk3.py\n",
164+
" gui-gtk4.py\n",
163165
" gui-pyglet.py\n",
164166
" gui-qt.py\n",
165167
" gui-tk.py\n",

examples/IPython Kernel/Rich Output.ipynb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3180,6 +3180,7 @@
31803180
"&nbsp;&nbsp;<a href='./gui/gui-glut.py' target='_blank'>gui-glut.py</a><br>\n",
31813181
"&nbsp;&nbsp;<a href='./gui/gui-gtk.py' target='_blank'>gui-gtk.py</a><br>\n",
31823182
"&nbsp;&nbsp;<a href='./gui/gui-gtk3.py' target='_blank'>gui-gtk3.py</a><br>\n",
3183+
"&nbsp;&nbsp;<a href='./gui/gui-gtk4.py' target='_blank'>gui-gtk4.py</a><br>\n",
31833184
"&nbsp;&nbsp;<a href='./gui/gui-pyglet.py' target='_blank'>gui-pyglet.py</a><br>\n",
31843185
"&nbsp;&nbsp;<a href='./gui/gui-qt.py' target='_blank'>gui-qt.py</a><br>\n",
31853186
"&nbsp;&nbsp;<a href='./gui/gui-tk.py' target='_blank'>gui-tk.py</a><br>\n",
@@ -3230,6 +3231,7 @@
32303231
" gui-glut.py\n",
32313232
" gui-gtk.py\n",
32323233
" gui-gtk3.py\n",
3234+
" gui-gtk4.py\n",
32333235
" gui-pyglet.py\n",
32343236
" gui-qt.py\n",
32353237
" gui-tk.py\n",

0 commit comments

Comments
 (0)