Python 3.4/GTK/异步

编程入门 行业动态 更新时间:2024-10-27 15:26:54
本文介绍了Python 3.4/GTK/异步的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我将tkinter与异步功能一起使用.

I use tkinter with a async funktion.

现在,我将使用gtk3代替tkinkter.

Now I will use gtk3 in stead of tkinkter.

还有运行我的异步功能的方法吗?

Is there also a way to run my async function?

我应该如何修改代码

以下是一些代码片段:

async def _event_loop(app, interval=0.05): try: while True: app.update() await asyncio.sleep(interval) except tkinter.TclError as exc: if "application has been destroyed" not in exc.args[0]: raise class SSHFrame(tkinter.Frame): def __init__(self, parent): super().__init__(parent) ... ... async def _run(self, host, command, user, password): try: async with asyncssh.connect(host, username=user, password=password, client_keys=None) as conn: self._proc = await conn.create_process(command, term_type='dumb') while not self._proc.stdout.at_eof(): self._output(await self._proc.stdout.read(1024)) self._output('\n[Disconnected]\n') except (asyncssh.Error, OSError) as exc: self._output('[%s]\n' % str(exc)) finally: self._proc = None class App(tkinter.Frame): def __init__(self, parent): super().__init__(parent) ... ...

asyncio.get_event_loop().run_until_complete(_event_loop(App(tkinter.Tk())))

asyncio.get_event_loop().run_until_complete(_event_loop(App(tkinter.Tk())))

推荐答案

import asyncio import sys from gi.repository import Gtk, GLib @asyncio.coroutine def start(app): yield from asyncio.sleep(0) app.register() app.activate() def glib_update(main_context, loop): while main_context.pending(): main_context.iteration(False) loop.call_later(.01, glib_update, main_context, loop) if sys.platform == "win32": from asyncio.windows_events import ProactorEventLoop loop = ProactorEventLoop() asyncio.set_event_loop(loop) else: loop = asyncio.SelectorEventLoop() asyncio.set_event_loop(loop) # This is just a fake gtk appliaction here, you should create your own see # python-gtk-3-tutorial.readthedocs.io/en/latest/application.html my_gtk_app = Gtk.Application() try: main_context = GLib.MainContext.default() asyncio.async(start(my_gtk_app)) glib_update(main_context, loop) loop.run_forever() finally: loop.close()

更多推荐

Python 3.4/GTK/异步

本文发布于:2023-11-23 11:07:02,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1621229.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Python   GTK

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!