限制 Tkinter 中的 GUI 更新。

huangapple go评论71阅读模式
英文:

Limiting the gui update in Tkinter

问题

Tkinter

我想要限制用户改变窗口大小时GUI更新的速度

我的计划是在"<Configure>"被调用后每隔0.5秒更新一次应用程序但我不知道如何实现它

```python
def wait_function():
    root.wait()  # 我不知道在这里该做什么。

root = tk.Tk()
root.bind("<Configure>", wait_function)
root.mainloop()
英文:

Tkinter:

I want to limit how fast the gui updates itself when the user is changing the size of the window.

My plan was to have the application update like every 0.5 sec after "<Configure&gt;", is called, but I don't know how to implement it.

def wait_function():
   root.wait()#i don&#39;t know what to do here.


root = tk.Tk()
root.bind(&quot;&lt;Configure&gt;&quot;, wait_function)
root.mainloop() 

答案1

得分: 0

以下是翻译好的部分:

简而言之:编写一个函数来执行需要完成的工作。每当收到&lt;Configure&gt;事件时,使用after计划执行该函数。在执行之前,请删除任何先前调用的调用。

注意:重要的是要意识到根窗口是唯一的。当你将任何事件绑定到根窗口时,该绑定将被根窗口中的每个小部件触发。因此,如果你希望绑定的函数只在为根窗口触发&lt;Configure&gt;事件时才调用,你需要在函数内部进行测试,以检查event.widget是否表示根窗口。

为了保持示例简洁,此示例使用全局变量。通常,我会建议在这种情况下使用类,因此我不建议在真实程序中使用这种编码风格。

代码中有一个名为count的变量,它初始化为零,然后在每次事件触发时更新。当程序启动时,标签将为空,直到初始&lt;Configure&gt;事件之后的500毫秒。之后,你可以手动调整窗口的大小,但只有在停止调整大小至少半秒后,函数才会被调用。这将反映在标签中的数字增加上。

import tkinter as tk

def after_configure():
    global count
    count += 1
    label.configure(text=f&quot;updated - {count}&quot;)

def schedule_work(event):
    global after_id
    if event.widget == root:
        if after_id:
            root.after_cancel(after_id)
        after_id = root.after(500, after_configure)

count = 0
after_id = None
root = tk.Tk()
label = tk.Label(root, text=&quot;&quot;, width=20)
label.pack(padx=10, pady=10)

root.bind(&quot;&lt;Configure&gt;&quot;, schedule_work)

root.mainloop()

注意:上面的代码示例是一个使用Python的Tkinter库创建GUI窗口应用程序的示例,它在窗口大小调整时执行某些操作。

英文:

In a nutshell: write a function to do whatever work needs to be done. Whenever you receive the &lt;Configure&gt; event, schedule the function using after. Before doing so, remove any previously called invocation.

Note: It's important to realize that the root window is unique. When you bind any event specifically to the root window, that binding will be triggered by every widget in the root window. Therefore, if you want the bound function to only be called when the &lt;Configure&gt; event has been triggered for the root window, you need to do a test inside the function to check that event.widget represents the root window.

To keep the example short, this example uses global variables. Normally I would recommend using a class for this type of thing, so I don't recommend this coding style in a real program.

The code has a variable named count which is initialized to zero, and then is updated each time the event is triggered. When the program starts up, the label will be blank until 500ms after the initial &lt;Configure&gt; event. After that, you can manually resize the window but the function will only be called once you've stopped resizing for at least half a second. You'll see that reflected in the number in the label being increased.

import tkinter as tk

def after_configure():
    global count
    count += 1
    label.configure(text=f&quot;updated - {count}&quot;)

def schedule_work(event):
    global after_id
    if event.widget == root:
        if after_id:
            root.after_cancel(after_id)
        after_id = root.after(500, after_configure)

count = 0
after_id = None
root = tk.Tk()
label = tk.Label(root, text=&quot;&quot;, width=20)
label.pack(padx=10, pady=10)

root.bind(&quot;&lt;Configure&gt;&quot;, schedule_work)

root.mainloop()

huangapple
  • 本文由 发表于 2023年6月26日 12:36:47
  • 转载请务必保留本文链接:https://go.coder-hub.com/76553550.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定