Python 异步编程:使用 TikTokLive 库获取礼物信息

Python Async Programming: Getting Gift info with TikTokLive Library

提问人:idiot one 提问时间:11/9/2023 更新时间:11/9/2023 访问量:24

问:

我正在尝试 TikTokLive 库来获取向我的 GUI 程序赠送礼物和饲料的用户名。但是我搞砸了如何使用异步函数。

TikTokLive 图书馆 https://github.com/isaackogan/TikTokLive

我的代码:

from TikTokLive import TikTokLiveClient
from TikTokLive.types.events import CommentEvent, ConnectEvent, GiftEvent
from PyQt5.QtWidgets import QApplication

    loop = asyncio.get_event_loop()
    client: TikTokLiveClient = TikTokLiveClient(unique_id="@" + streamername, loop=loop)
    
    @client.on("connect")
    async def on_connect(_: ConnectEvent):
        print("Connected to Room ID:", client.room_id)
    
    async def on_comment(event: CommentEvent):
        print(f"{event.user.nickname} -> {event.comment}")
    
    client.add_listener("comment", on_comment)
    
    async def main():
        print("async main")
        await client.start()
    loop.run_until_complete(main())
    
    app = QApplication(sys.argv)
    # My rest GUI program that needs the username who gives gift

程序可以运行,但控制台不显示赠送礼物的用户名。如果我在 TikTokLive 中使用原始示例,我可以获得用户名。

Python 异步 TikTok

评论

0赞 Tim Roberts 11/9/2023
loop.run_until_complete不返回。您的 GUI 永远不会启动。右?
0赞 idiot one 11/9/2023
GUI 已成功启动,但我无法在命令行中获取礼物信息

答: 暂无答案