我目前正在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并检索数据.我认为这是导致图形用户界面变得迟缓的原因.有什么办法解决这个问题吗?