tkinter标签更新但不正确 [英] tkinter Label updating but not correctly

查看:114
本文介绍了tkinter标签更新但不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Python 3.5.我有一个tkinter表格.用户单击一个按钮以将许多文件导入到Listbox中.另一个按钮可循环浏览文件,并从文件中读取和提取数据.

Using Python 3.5. I have a tkinter form. The user clicks a button to import many files into a Listbox. Another button loops thru the files and reads and extracts data from them.

我在表单上有一个Label,它指示循环的状态.除了在末尾添加了额外的字符外,该状态在大多数情况下都可以按预期工作.我不确定这些字符来自哪里.我还将print()的内容与Label相同,返回到屏幕,并且print()完全显示了它应该显示的内容.

I have a Label on the form that indicates the status of the loop. The status, for the most part, works as expected except that extra characters are added on the end. I'm not sure where the characters come from. I also print() the same content as the Label back to the screen and the print() displays exactly what it should.

我的问题是为什么我的Label没有显示正确的字符串?

My question is why is my Label not displaying the correct string?

我的代码,大大缩短了:

My code, greatly shortened:

class tk_new_db:
    def __init__(self, master):
        self.master = master  # sets 'root' to the instance variable 'master'
        self.var_text_2 = StringVar()
        self.var_text_2.set('STATUS: Active')
        self.label_6 = Label(master, textvariable=self.var_text_2,
                             font=self.font_10)
        self.label_6.grid(row=15, sticky=W, padx=15)

    def execute_main(self):  # extract data from files
        file_num = 0
        nf = len(self.listbox_1.get(0, END))
        for fr in li:
            file_num += 1
            print('STATUS: Extracting Loads from File '
                  '{} in {}'.format(file_num, nf))
            self.var_text_2.set('STATUS: Extracting Loads from File '
                                '{} in {}'.format(file_num, nf))
            self.master.update_idletasks()

print()正在编写以下内容: STATUS: Extracting Loads from File 1 in 5

The print() is writing the following: STATUS: Extracting Loads from File 1 in 5

Label正在编写以下内容: STATUS: Extracting Loads from File 1 in 5 nce...

The Label is writing the following: STATUS: Extracting Loads from File 1 in 5 nce...

它总是在表单上添加' nce...'.

我确实在程序的前面使用了self.var_text_2. ' nce...'看起来像是前一个字符串的片段.从那以后,我尝试用两种不同的方式重置变量,但是仍然得到相同的结果.

I do use self.var_text_2 earlier in the program. The ' nce...' looks like it is a fragment of the previous string. I've since tried resetting the variable in two different ways, but I'm still getting the same result.

    self.var_text_2.set('STATUS: Checking .F06 Files for Convergence...')
    self.master.update_idletasks()
    self.var_text_2.__del__()

    self.var_text_2.set('STATUS: Checking .F06 Files for Convergence...')
    self.master.update_idletasks()
    self.var_text_2.set('')

您如何正确删除StringVar()以便重复使用?

How do you properly delete the StringVar() for reuse?

推荐答案

我想我可以解释. Root.mainloop反复调用root.update. Root.update在主"事件队列上执行任务.简而言之,它仅在事件队列为空后在 之后执行空闲任务,检查文件事件(在非Windows上),window-gui事件和计时器事件.空闲任务(没有充分记录,但似乎主要是屏幕更新任务)从未添加到主事件队列中.因此,闲置任务有可能无限期地保持撤销状态.存在Root.update_idletasks可以强制执行空闲任务.

I think I can explain. Root.mainloop repeatedly calls root.update. Root.update executes tasks on the 'main' event queue. In brief, it only executes idletasks when the event queue is empty after checking for file events (on non-Windows), window-gui events, and timer events. Idletasks, which are not well documented but seem to primarily be screen update tasks, are never added to the main event queue. It is therefore possible that idletasks will remain undone indefinitely. Root.update_idletasks exists to force execution of idletasks anyway.

在运行mainloop时,通常不需要在由update运行的任务中调用update,并且可能更糟.这仅适用于冒险专家.因此,您已阅读警告(假设您正在运行mainloop).

When mainloop is running, calling update within a task run by update is usually unnecessary and possibly worse. It is something only for adventurous experts. Hence the warnings you have read (which assume that you are running mainloop).

当mainloop没有运行并且没有重复调用update时,要么是因为您从未调用过mainloop,要么是因为您长时间运行了该循环而将其阻塞,那么您可能必须自己调用update.您似乎已经发现,完全处理StringVar更新的一部分是主要事件,而不仅仅是一个空闲任务.

When mainloop is not running and update is not being called repeatedly, either because you never called mainloop or because you block it with a long running loop, then you likely must call update yourself. What you seem to have discovered is that part of completely handling a StringVar update is a main event and not just an idletask.

我对您希望通过for循环执行重复任务的愿望表示同情,但是目前这样做意味着您要自己负责事件处理.我希望将来可以与Tk.mainloop进行异步"(3.5中的新增功能)互操作,但这并不像我希望的那么容易.

I sympathize with your desire to do repeated tasks with a for loop, but doing so currently means taking responsibility for event handling yourself. I hope in the future that it will be possible to have 'async for' (new in 3.5) interoperate with Tk.mainloop, but it is not as easy as I hoped.

同时,您可以将for循环的主体放入回调中,并使用root.after循环.

In the meanwhile, you could put the body of the for loop into a callback and loop with root.after.

这篇关于tkinter标签更新但不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆