altdesktop / i3ipc-python

🐍 An improved Python library to control i3wm and sway.
http://i3ipc-python.readthedocs.io
BSD 3-Clause "New" or "Revised" License
851 stars 109 forks source link

combine i3ipc with other event loops #134

Open ghistes opened 4 years ago

ghistes commented 4 years ago

Hi,

is it possible (and if it is how do you do it) to combine the event-loop from i3ipc with another event loop (gtk say) so that you could for example write a GUI-script that uses i3ipc?

Many thanks!

acrisci commented 4 years ago

No, it's not currently possible. But I would like to support the use case and I'm open to a discussion about how to implement it.

acrisci commented 4 years ago

For GTK in particular, I have this project but I haven't updated in awhile.

ghistes commented 4 years ago

I can't really contribute to any discussion as I am pretty new to Python and don't know the ecosystem. But it maybe worth you can get some ideas from Perl's AnyEvent-module (https://metacpan.org/pod/AnyEvent).

Aphray commented 4 years ago

I realize this is a slightly old thread, however I felt I should add that it is possible to combine with the event loop of Qt using asyncqt. The snippet below achieves just that.

from i3ipc.aio import Connection
from i3ipc import Event
from PyQt5.QtWidgets import QApplication, QWidget
from asyncqt import QEventLoop
import sys
import asyncio

def _print(conn, event):
    print(event)

async def main():
    i3 = await Connection(auto_reconnect=True).connect()
    i3.on(Event.WINDOW, _print)
    await i3.main()

if __name__ == '__main__':
    app = QApplication(sys.argv)
    loop = QEventLoop(app)
    asyncio.set_event_loop(loop)
    widget = QWidget()
    widget.show()
    with loop:
        sys.exit(loop.run_until_complete(main()))

In case anyone is looking to combine with Qt.