按下按钮时没有冻结TKINTER的解决方案

发布于 2025-02-11 06:26:49 字数 2569 浏览 0 评论 0原文

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

示例代码下面:

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
    # from TikTokLive.types.events import
    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 :  # if live is ended
        print('finished')

mainloop()

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

python在后台运行任务,同时允许TKINTER处于活动状态

tkinter窗口说(不响应),但代码正在运行

如何轻松避免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 *
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
    # from TikTokLive.types.events import
    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 :  # if live is ended
        print('finished')

mainloop()

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技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文