##// END OF EJS Templates
ENH: support for `PySide6` in `%gui` (#13864)...
ENH: support for `PySide6` in `%gui` (#13864) Addresses #13859 Changes here parallel the changes in [ipykernel](https://github.com/ipython/ipykernel/pull/1054), i.e. prefer `PyQt` over `PySide` and allow requesting explicit versions (e.g. 'qt5') with one difference (see below). I believe that, eventually, the Qt importing logic should be all here, and `ipykernel` should defer to`ipython`. I chose not to do that at this time since it would mean the latest `ipykernel` would require the latest `IPython`; I'm happy to discuss further. The only difference between `IPython` and `ipykernel`, after these two pull requests, is that, in `IPython`, it's not possible to explicitly request an event loop for Qt4. This is because an alias exists [here](https://github.com/ipython/ipython/blob/5409de68d87ddd073a35111aca0cb8360ff63ca8/IPython/terminal/pt_inputhooks/__init__.py#L5) which effectively makes "qt4" be "the latest Qt available". I did not remove the alias because I don't know the history behind it.

File last commit:

r28021:b332fd1a
r28120:0374cf80 merge
Show More
eventloops.rst
108 lines | 4.3 KiB | text/x-rst | RstLexer

Integrating with GUI event loops

When the user types %gui qt, IPython integrates itself with the Qt event loop, so you can use both a GUI and an interactive prompt together. IPython supports a number of common GUI toolkits, but from IPython 3.0, it is possible to integrate other event loops without modifying IPython itself.

Supported event loops include qt5, qt6, gtk2, gtk3, gtk4, wx, osx and tk. Make sure the event loop you specify matches the GUI toolkit used by your own code.

To make IPython GUI event loop integration occur automatically at every startup, set the c.InteractiveShellApp.gui configuration key in your IPython profile (see :ref:`setting_config`).

If the event loop you use is supported by IPython, turning on event loop integration follows the steps just described whether you use Terminal IPython or an IPython kernel.

However, the way Terminal IPython handles event loops is very different from the way IPython kernel does, so if you need to integrate with a new kind of event loop, different steps are needed to integrate with each.

Integrating with a new event loop in the terminal

In the terminal, IPython uses prompt_toolkit to prompt the user for input. prompt_toolkit provides hooks to integrate with an external event loop.

To integrate an event loop, define a function which runs the GUI event loop until there is input waiting for prompt_toolkit to process. There are two ways to detect this condition:

# Polling for input.
def inputhook(context):
    while not context.input_is_ready():
        # Replace this with the appropriate call for the event loop:
        iterate_loop_once()

# Using a file descriptor to notify the event loop to stop.
def inputhook2(context):
    fd = context.fileno()
    # Replace the functions below with those for the event loop.
    add_file_reader(fd, callback=stop_the_loop)
    run_the_loop()

Once you have defined this function, register it with IPython:

Integrating with a new event loop in the kernel

The kernel runs its own event loop, so it's simpler to integrate with others. IPython allows the other event loop to take control, but it must call :meth:`IPython.kernel.zmq.kernelbase.Kernel.do_one_iteration` periodically.

To integrate with this, write a function that takes a single argument, the IPython kernel instance, arranges for your event loop to call kernel.do_one_iteration() at least every kernel._poll_interval seconds, and starts the event loop.

Decorate this function with :func:`IPython.kernel.zmq.eventloops.register_integration`, passing in the names you wish to register it for. Here is a slightly simplified version of the Tkinter integration already included in IPython:

@register_integration('tk')
def loop_tk(kernel):
    """Start a kernel with the Tk event loop."""
    from tkinter import Tk

    # Tk uses milliseconds
    poll_interval = int(1000*kernel._poll_interval)
    # For Tkinter, we create a Tk object and call its withdraw method.
    class Timer(object):
        def __init__(self, func):
            self.app = Tk()
            self.app.withdraw()
            self.func = func

        def on_timer(self):
            self.func()
            self.app.after(poll_interval, self.on_timer)

        def start(self):
            self.on_timer()  # Call it once to get things going.
            self.app.mainloop()

    kernel.timer = Timer(kernel.do_one_iteration)
    kernel.timer.start()

Some event loops can go one better, and integrate checking for messages on the kernel's ZMQ sockets, making the kernel more responsive than plain polling. How to do this is outside the scope of this document; if you are interested, look at the integration with Qt in :mod:`IPython.kernel.zmq.eventloops`.