且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Python Tkinter 标签每 10 秒重绘一次

更新时间:2023-02-25 14:05:04

在 tkinter 中运行函数或更新标签的正确方法是使用 after 方法.这会将事件放在事件队列中,以便在将来的某个时间执行.如果你有一个函数做了一些工作,然后把自己放回事件队列,你就创建了一个永远运行的东西.

The correct way to run a function or update a label in tkinter is to use the after method. This puts an event on the event queue to be executed at some time in the future. If you have a function that does some work, then puts itself back on the event queue, you have created something that will run forever.

以下是基于您的示例的快速示例:

Here's a quick example based on your example:

import Tkinter as tk
import time

def Draw():
    global text

    frame=tk.Frame(root,width=100,height=100,relief='solid',bd=1)
    frame.place(x=10,y=10)
    text=tk.Label(frame,text='HELLO')
    text.pack()

def Refresher():
    global text
    text.configure(text=time.asctime())
    root.after(1000, Refresher) # every second...

root=tk.Tk()
Draw()
Refresher()
root.mainloop()

从编码风格的角度来看,我会对该程序进行很多更改,但我想让它尽可能接近您的原始问题.关键是,您可以使用 after 调用更新标签的函数,而无需创建新标签.另外,该函数可以安排自己在某个时间间隔再次被调用.在这个例子中,我选择了一秒钟,以便更容易看到效果.

There are a lot of things I would change about that program from a coding style point of view, but I wanted to keep it as close to your original question as possible. The point is, you can use after to call a function that updates the label without having to create new labels. Plus, that function can arrange for itself to be called again at some interval. In this example I picked one second just so that the effect is easier to see.

您还问我想知道我不应该使用线程的一般情况是什么,我可以使用线程的一般情况是什么?"

You also asked "I am wondering what are the general cases that i should not use threading and what are the general cases i could use threading?"

坦率地说,如果您必须询问何时使用线程的问题,则永远不要使用线程.线程是一种高级技术,它很复杂,而且很容易出错.线程很可能使您的程序变慢而不是变快.它会产生微妙的后果,例如,如果您执行非线程安全的操作,您的程序就会神秘地失败.

To put a blunt point on it, you should never use threading if you have to ask a question about when to use threading. Threading is an advanced technique, it is complicated, and it easy to get wrong. It's quite possible for threading to make your program slower rather than faster. It has subtle consequences, such as your programs failing mysteriously if you do things that aren't thread safe.

更具体地说明您的情况:您应该避免在 tkinter 中使用线程.您可以使用它们,但是您不能从这些其他线程访问小部件.如果您需要对小部件执行某些操作,则必须将指令放入线程安全队列中,然后在主线程中您需要定期检查该队列以查看是否有要处理的指令.如果您搜索它们,本网站上有这样的示例.

To be more specific to your situation: you should avoid using threads with tkinter. You can use them, but you can't access widgets from these other threads. If you need to do something with a widget, you must put an instruction in a thread-safe queue, and then in the main thread you need to periodically check that queue to see if there's an instruction to be processed. There are examples of that on this website if you search for them.

如果这一切听起来很复杂,那就是.对于您编写的大多数 GUI,您无需担心.如果您可以处理大型进程并将它们分解为在 100 毫秒或更短的时间内执行的块,那么您只需要 after,而永远不需要线程.

If all that sounds complicated, it is. For most of the GUIs you write, you won't need to worry about that. If you can take large processes and break them down into chunks that execute in 100 ms or less, you only need after, and never need threads.