tkinter冻结当按按钮与tiktoklive之类的库时

发布于 2025-02-10 19:28:21 字数 2665 浏览 2 评论 0 原文

我面临一个问题,我想了解为什么会发生这个问题以及如何解决这个问题。我知道堆栈溢出的很多问题,这些问题在按下按钮时谈论了tkinter冻结,我看到了许多解决方案,但它对我不起作用,我想要一些帮助来了解如何解决此问题,我使用tkinter我尝试了一些应用程序使用诸如tiktoklive之类的库来学习新的东西,我尝试制作一个按钮,当按下时,我尝试了一些我尝试过的信息螺纹,但不使用,并且试图看到有关使用queue的信息,我了解 queue < /strong>和我看到了使用多流程之类的解决方案,但对我没有任何作用,所以我认为由于tiktoklive库而发生的问题可能是因为我们使用 async 编程,所以我做了有些搜索和看到解决方案但不起作用,我想提供更多帮助,以了解如何解决此问题。

示例代码下面:

from tkinter import *

root = Tk()
root.geometry("300x300")

get_information = Button(text="get", font='normal 30 bold', command=lambda: connect_with_tiktok_live())
get_information.pack()

def connect_with_tiktok_live():
    # dependencies
    import TikTokLive.types.events
    from TikTokLive import TikTokLiveClient
    from TikTokLive.types.events import ConnectEvent
    username = "put the username of tiktok live"

    # Instantiate the client with the user's username
    client: TikTokLiveClient = TikTokLiveClient(
        unique_id=username, **({
            "process_initial_data": False, 
            "enable_extended_gift_info": True,
        }))

    @client.on('connect')
    async def on_connect(_: ConnectEvent):
        print("Connected to Room ID: ", client.room_id)

    # Run the client and block the main thread
    # await client.start() to run non-blocking
    try:
        return client.run()
    except FailedConnection:  # if live is ended
        print('finished')
  
mainloop()

gif problrm:

一些链接解决方案不适合我

在运行异步任务时,我如何防止tkinter gui冻结?

< a href =“ https://stackoverflow.com/questions/185222171/tkinter-window-says-not-responding-but-code-isponding-code-is-running/68897016#68897016”

如何轻松避免tkinter冷冻?

如何在tkinter的背景下运行一个函数

tkinter:如何使用线程来防止主事件循环循环循环来自“冷冻”

I face an issue and I want to understand why this problem happens and how to solve this problem. I know a lot of questions on stack overflow that talk about tkinter freeze when button pressed I saw many solutions but it is not working for me and I want some help to understand how to solve this problem, I work on some app using tkinter I trying use libraries like TikTokLive to learn new something and I tried to make a button that when pressed gives some information I tried threading but not working and tried to see about use queue and I understand the queue and I saw solutions like use multiprocess but nothing works for me so I thought that problem happens because of the tiktoklive library may be because we use async programming so I made some search and saw solution but not working, I want to some help to understand more how to solve this problem.

Sample code below:

from tkinter import *

root = Tk()
root.geometry("300x300")

get_information = Button(text="get", font='normal 30 bold', command=lambda: connect_with_tiktok_live())
get_information.pack()

def connect_with_tiktok_live():
    # dependencies
    import TikTokLive.types.events
    from TikTokLive import TikTokLiveClient
    from TikTokLive.types.events import ConnectEvent
    username = "put the username of tiktok live"

    # Instantiate the client with the user's username
    client: TikTokLiveClient = TikTokLiveClient(
        unique_id=username, **({
            "process_initial_data": False, 
            "enable_extended_gift_info": True,
        }))

    @client.on('connect')
    async def on_connect(_: ConnectEvent):
        print("Connected to Room ID: ", client.room_id)

    # Run the client and block the main thread
    # await client.start() to run non-blocking
    try:
        return client.run()
    except FailedConnection:  # if live is ended
        print('finished')
  
mainloop()

gif problrm:

enter image description here

some links solution not working for me

How can I prevent a tkinter Gui from freezing while an async task is running?

python running task in the background while allowing tkinter to be active

Tkinter window says (not responding) but code is running

How to easily avoid Tkinter freezing?

How to run a function in the background of tkinter

Tkinter: How to use threads to preventing main event loop from "freezing"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南七夏 2025-02-17 19:28:21

您可以在此处使用 threading.thread 方法。

from tkinter import *
from threading import Thread
root = Tk()
root.geometry("300x300")

get_information = Button(text="get", font='normal 30 bold', command= lambda:Thread(target=connect_with_tiktok_live).start())
get_information.pack()


def connect_with_tiktok_live():
    # dependencies
    import TikTokLive.types.events
    from TikTokLive import TikTokLiveClient
    from TikTokLive.types.events import ConnectEvent
    username = "put the username of tiktok live"

    # Instantiate the client with the user's username
    client: TikTokLiveClient = TikTokLiveClient(
        unique_id=username, **({
            "process_initial_data": False, 
            "enable_extended_gift_info": True,
        }))

    @client.on('connect')
    async def on_connect(_: ConnectEvent):
        print("Connected to Room ID: ", client.room_id)

    # Run the client and block the main thread
    # await client.start() to run non-blocking
    try:
        return client.run()
    except FailedConnection:  # if live is ended
        print('finished')
  
mainloop()

You can use multithreading here using threading.Thread method.

from tkinter import *
from threading import Thread
root = Tk()
root.geometry("300x300")

get_information = Button(text="get", font='normal 30 bold', command= lambda:Thread(target=connect_with_tiktok_live).start())
get_information.pack()


def connect_with_tiktok_live():
    # dependencies
    import TikTokLive.types.events
    from TikTokLive import TikTokLiveClient
    from TikTokLive.types.events import ConnectEvent
    username = "put the username of tiktok live"

    # Instantiate the client with the user's username
    client: TikTokLiveClient = TikTokLiveClient(
        unique_id=username, **({
            "process_initial_data": False, 
            "enable_extended_gift_info": True,
        }))

    @client.on('connect')
    async def on_connect(_: ConnectEvent):
        print("Connected to Room ID: ", client.room_id)

    # Run the client and block the main thread
    # await client.start() to run non-blocking
    try:
        return client.run()
    except FailedConnection:  # if live is ended
        print('finished')
  
mainloop()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文