Tkinter 方法 deiconify 似乎不适用于 ubuntu(12.04, unity) [英] Tkinter method deiconify seems not working on ubuntu(12.04, unity)

查看:25
本文介绍了Tkinter 方法 deiconify 似乎不适用于 ubuntu(12.04, unity)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Tkinter 方法deiconify"似乎不适用于 ubuntu(12.04, unity),但以下代码在 Windows 7 上按预期工作.我需要显示窗口,即使它在另一个项目中发生某些事情时最小化https://github.com/thinker3/youdao.

Tkinter method "deiconify" seems not working on ubuntu(12.04, unity), but the the following code works as expected on windows 7. I need to show the window even it is minimized when something happens in another project https://github.com/thinker3/youdao.

from time import sleep
from Tkinter import Tk

class GUI():
    def __init__(self):
        self.root = Tk()
        self.root.title("Test")
        self.root.protocol("WM_DELETE_WINDOW", self.close_handler)
        self.root.mainloop()

    def close_handler(self):
        self.root.iconify()
        sleep(1)
        self.root.deiconify()


if __name__ == '__main__':
    gui = GUI()

推荐答案

您所看到的显然是 Windows 和非 Windows 之间的区别.在我看来,Windows 行为是一个错误.至少,这不是预期的行为.您在 ubuntu 上看到的正是我希望看到的.

What you are seeing is apparently the difference between Windows and non-Windows. It looks to me like the Windows behavior is a bug. At the very least, it's not expected behavior. What you are seeing on ubuntu is what I would expect to see.

要使 GUI 执行任何操作,必须运行事件循环.发生的一切都是对事件的响应.在屏幕上绘制窗口是对事件的响应.单击按钮是对事件的响应.按下按钮的视觉效果是对事件的响应.等等.

For a GUI to do anything, the event loop must be running. Everything that happens is the response to an event. The drawing of a window on the screen is a response to an event. A button click is a response to an event. The visual effect of a button being pressed is a response to an event. And so on.

当您调用 iconify 时,这会导致一个事件发送到应用程序,说从屏幕上移除窗口".事件循环看到事件,并重新绘制(或取消"绘制)窗口.当你调用 deiconify 时会发生相反的情况——系统得到一个重绘事件,tkinter 在屏幕上重绘窗口.

When you call iconify, that causes an event to be sent to the app saying "remove the window from the screen". The event loop sees the event, and redraws (or "un"draws) the window. The reverse happens when you call deiconify -- the system gets a redraw event, and tkinter redraws the window on the screen.

在您的代码中,您永远不会给事件循环响应这些事件的机会.你要求它图标化,然后你睡觉,然后你去图标化,所有这一切都没有给事件循环响应的机会.当 sleep 命令正在运行时,没有 事件被处理.所以,当你从睡眠中醒来时,系统会隐藏窗口,然后在几微秒后重新绘制它.

In your code, you never give the event loop a chance to respond to these events. You ask it to iconfiy, then you sleep, then you deiconify, all without giving the event loop a chance to respond. While the sleep command is running no events are processed. So, when you wake from sleep, the system hides the window, and then microseconds later it redraws it.

Windows 上可能发生的事情是窗口管理器正在获取 iconfiy 命令并在没有 tkinter 参与的情况下删除窗口.换句话说,tkinter 实际上并没有做任何事情来让它消失.然而,在基于 X11 的系统上,这一切都由事件循环管理.

What is probably happening on windows is that the window manager is getting the iconfiy command and removing the window without tkinter's involvement. In other words, tkinter doesn't actually do anything to make it go away. On X11-based systems, however, this is all managed by the event loop.

如果您希望窗口消失,并在一秒钟后重新出现,请使用事件循环对您有利.允许事件循环更新屏幕,并安排在将来的某个时间进行去图标化.例如:

If you want the window to go away, and a second later reappear, use the event loop to your advantage. Allow the event loop to update the screen, and arrange for the deiconify to happen at sometime in the future. For example:

def close_handler(self):
    self.root.iconify()
    self.after(1000, self.root.deiconify)

这应该适用于所有平台.它允许事件循环响应 iconify 事件,然后在一秒钟后运行 deiconify 命令.

This should work on all platforms. It allows the event loop to respond to the iconify event, and then a second later the deiconify command will run.

这篇关于Tkinter 方法 deiconify 似乎不适用于 ubuntu(12.04, unity)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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