`<tkinter.Tk>.iconify()` 总是需要 2 秒 [英] `<tkinter.Tk>.iconify()` always takes 2 seconds

查看:24
本文介绍了`<tkinter.Tk>.iconify()` 总是需要 2 秒的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我想 iconify 一个 tkinter 窗口.我会使用这样的东西:

Let's say that I want to iconify a tkinter window. I would use something like this:

from time import perf_counter
import tkinter as tk

def time(function, *args, **kwargs):
    start = perf_counter()
    function(*args, **kwargs)
    return perf_counter() - start

root = tk.Tk()
root.update() # Make sure that the window appears on the screen
print(time(root.iconify)) # 2.0016179770000235

问题在于 .iconify() 的运行时间总是超过 2 秒.这是为什么?我查看了 tkinters源代码:

The problem is that it always takes a bit more than 2 seconds for the .iconify() to run. Why is that? I looked at tkinter's source code:

def wm_iconify(self):
    """Display widget as icon."""
    return self.tk.call('wm', 'iconify', self._w)

iconify = wm_iconify

并且它直接用参数调用_tkinter.我尝试阅读 _tkinter 的源代码(here) 但我找不到 2 秒去哪里的踪迹.我的猜测是问题出在 _tkinter 下方.

and it directly calls _tkinter with the parameters. I tried reading _tkinter's source code (here) but I can't find a trace for where the 2 seconds are going. My guess is that the problem is somewhere bellow _tkinter.

编辑:此问题仅影响 Python 3.8.6 + Ubuntu 20.10,但不会出现在 Python 3.7.9 上 + Window 10.

Edit: This issue only effects Python 3.8.6 + Ubuntu 20.10 but doesn't appear on Python 3.7.9 + Window 10.

推荐答案

Unix 上的 Tk 实现的最底层(中间还有其他部分,其中很多,但它们都非常快)del> X11 是 这个,为了您的方便,复制在这里:

At the very bottom (there's other bits in between, many of them, but they're all really fast) of Tk's implementation on Unix X11 is this, reproduced here for your convenience:

if (XIconifyWindow(winPtr->display, wmPtr->wrapperPtr->window,
        winPtr->screenNum) == 0) {
    return 0;
}
WaitForMapNotify(winPtr, 0);

(WaitForMapNotify 很复杂,但是按照它所说的去做:等待来自 X11 的 MapNotify 事件.)

(The WaitForMapNotify is complex, but does what it says on the tin: waiting for a MapNotify event from X11.)

它发送一个图标化请求,然后等待窗口管理器响应.出于某种原因,Ubuntu 需要很长时间(2 秒!)来发送该消息.我会将其描述为 Ubuntu 中的一个错误……除非出于某种原因您启用了一些奇特的动画并且窗口管理器仅在执行它们后才完成状态更改.

It sends a request to iconify and then waits for the Window manager to respond. For some reason, Ubuntu is taking a very long time (2 seconds!) to send that message. I'd describe that as a bug in Ubuntu… unless for some reason you've got some fancy animations enabled and the Window Manager is only finalizing the state change after performing them.

这篇关于`<tkinter.Tk>.iconify()` 总是需要 2 秒的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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