如何测量绘制窗口所需的时间?

问题描述 投票:0回答:1

我们都经历过响应式系统和滞后系统之间的区别。输入延迟。当您点击时,它的响应速度不够快。当您键入一个字符时,它需要很长时间才能显示(几分之一秒)。

我编写了这段代码来测量绘制窗口需要多长时间,但我认为它不准确。

如何测量按下某个键后该键需要多长时间才能在屏幕上可见?


import tkinter as tk
import time

# Create a window
window = tk.Tk()

# Set the title of the window
window.title("Basic Window")

# Set the size of the window
window.geometry("300x200")

# Start the timer
start_time = time.time()

# Update the window and measure the time until it's rendered
while not window.winfo_viewable():
    window.update_idletasks()

elapsed_time = time.time() - start_time
print("Elapsed time to render the window: {} seconds".format(elapsed_time))

# Run the window's event loop
window.mainloop()
python linux performance desktop lag
1个回答
0
投票

简单来说,你不能。这些是事件驱动的系统。脚本中的所有命令直到最后一个命令根本不执行任何绘图。除了一些例外,Tk 调用不会进行任何绘图。他们所做的就是将消息发布到线程的消息队列中。最后,当

window.mainloop
启动时,它开始从该队列中弹出消息并将它们分派到执行实际绘图的 UI 组件。

制作响应式 UI 的关键是确保尽快返回消息循环。只要回调很快,这些消息就可以被调度,并且事情将会被绘制。

© www.soinside.com 2019 - 2024. All rights reserved.