简体   繁体   English

Tkinter,为什么我的代码跳过第一个标签更新?

[英]Tkinter, why does my code skip the first label update?

I have a button that I want to press to process some excel data. 我有一个要按一下以处理一些excel数据的按钮。 This often takes a moment or two especially if the user uploads a number of large files. 这通常需要一两分钟的时间,特别是如果用户上载大量大文件。 While the files are being processed, I want to display a note to the user that the files are processing and that the program has not frozen. 在处理文件时,我想向用户显示文件正在处理且程序未冻结的注释。 Once the files are complete, I want to indicate that as well. 文件完成后,我也要指出这一点。

This is the relevant code I have so far: 这是我到目前为止的相关代码:

    self.l2 = Label(self, text = " ", width = 20)
    self.l2.grid(row = 3, column = 2, sticky = W)

    # Calculate button
    self.b2 = Button(self, text = "Calculate", command = self.calculate, width = 20)
    self.b2.grid(row = 3, column = 3, sticky = W)


def calculate(self):
    self.l2.config(text="Processing...")
    get_data(filelist, self.v.get())
    self.l2.config(text="Files Ready!")

The label is initially blank like I want and once the files are ready it shows "files ready" but it never seems to hit the "processing" label even if I upload enough files for the get_data command to take nearly a minute. 标签最初像我想要的一样是空白,一旦文件准备好,它会显示“文件就绪”,但是即使我上传了足够的文件以使get_data命令花费将近一分钟,它也似乎从未打过“处理中”标签。

Why exactly would it not be showing up? 为什么不显示呢? It seems like the commands should execute sequentially, the label should update, get_data should be called, then once get _data has returned, the next label update should happen. 似乎命令应该顺序执行,标签应该更新,应该调用get_data,然后一旦get_data返回,就应该进行下一个标签更新。 Is this logic incorrect? 这个逻辑不正确吗?

In order for the screen to refresh, the event loop must be able to process a refresh event. 为了刷新屏幕,事件循环必须能够处理刷新事件。 Since your code is running, the event loop is frozen. 由于您的代码正在运行,因此事件循环被冻结。

A quick and dirty solution is to call self.update_idletasks() , which tells tkinter to handle all of the "idle" events, which includes events related to updating the screen. 一种快速而肮脏的解决方案是调用self.update_idletasks() ,它告诉self.update_idletasks()处理所有“空闲”事件,其中包括与更新屏幕有关的事件。

self.l2.config(text="Processing...")
self.update_idletasks()
get_data(filelist, self.v.get())
self.l2.config(text="Files Ready!")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM