我目前正在try 在我的tkinter图形用户界面上更新标签的文本,我从一个调用API和检索数据的单独函数获取更新的标签文本.然而,我注意到,虽然标签的文本通过After()成功更新,但图形用户界面变得非常迟缓和没有响应,在我将其拖过屏幕后,窗口需要3-4秒才能移动.下面是我的代码中与之相关的部分.我已经将其修改为在单独的线程上运行tkinter,但我对此不太确定,因为我对多线程还不熟悉.如果有任何建议,我将不胜感激.

class App(threading.Thread,):
    def __init__(self):
        threading.Thread.__init__(self)
        self.start()

    def callback(self):
        self.root.quit()

    def update_top_coin_list(self, update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list):
        usdt_krw = get_usdt_krw_yfinance()
        for (coin_button, coin_kimchi_enter, coin_kimchi_exit) in zip(update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list):
            coin_kimchi_enter['text'], coin_kimchi_exit['text'] = get_info_single_coin(coin_button['text'], usdt_krw, 'Binance')
        self.root.after(1000, self.update_top_coin_list, update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list)

    def run(self):
        self.root = tk.Tk()
        self.root.protocol("WM_DELETE_WINDOW", self.callback)

        self.root.geometry("900x225")
        self.root.resizable(0, 0)

        # Redacted code here for frames/widgets/etc not super relevant to the issue

        # Run first time
        self.update_top_coin_list(coin_button_list, coin_kimchi_enter_list, coin_kimchi_exit_list)

        self.root.mainloop()

您可以看到,在每1秒运行一次的UPDATE_TOP_COUNT_LIST中调用GET_INFO_SINGLE_COUNT函数.列表本身非常短(5个值),所以这不是问题所在,而是GET_INFO_SINGLE_COUNT函数调用API并检索数据.我认为这是导致图形用户界面变得迟缓的原因.有什么办法解决这个问题吗?

推荐答案

功能get_info_single_coin从API检索数据,网络请求可能是耗时的,并且在可能导致无响应的图形用户界面内.

让我们在单独的线程中执行这些API调用.

def update_top_coin_list(self, update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list):
    def thread_update(coin_button, coin_kimchi_enter, coin_kimchi_exit):
        usdt_krw = get_usdt_krw_yfinance()
        coin_kimchi_enter['text'], coin_kimchi_exit['text'] = get_info_single_coin(coin_button['text'], usdt_krw, 'Binance')
        self.root.after(1000, self.update_top_coin_list, update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list)

    for coin_button, coin_kimchi_enter, coin_kimchi_exit in zip(update_ticker_label_list, update_kimp_enter_label_list, update_kimp_exit_label_list):
        threading.Thread(target=thread_update, args=(coin_button, coin_kimchi_enter, coin_kimchi_exit)).start()

Python相关问答推荐

Python 枕头上的图像背景变黑

Python(Polars):使用之前的变量确定当前解决方案的Vector化操作

保留包含pandas pandras中文本的列

Pandas read_jsonfuture 警告:解析字符串时,to_datetime与单位的行为已被反对

如何编写一个正规表达式来查找序列中具有2个或更多相同辅音的所有单词

如何将带有逗号分隔的数字的字符串解析为int Array?

如何使用Selenium访问svg对象内部的元素

在for循环中仅执行一次此操作

通过交换 node 对链接列表进行 Select 排序

Pandas :多索引组

返回nxon矩阵的diag元素,而不使用for循环

由于NEP 50,向uint 8添加-256的代码是否会在numpy 2中失败?

连接两个具有不同标题的收件箱

将jit与numpy linSpace函数一起使用时出错

Python,Fitting into a System of Equations

pyscript中的压痕问题

ThreadPoolExecutor和单个线程的超时

Pandas DataFrame中行之间的差异

根据列值添加时区

在www.example.com中使用`package_data`包含不包含__init__. py的非Python文件