python - root.overrideredirect 和 <Any-KeyPress> 绑定(bind)

标签 python events tkinter binding

我希望以下程序在 <Any-KeyPress> 上退出事件。

from tkinter import *

root = Tk()
root.overrideredirect(True)
root.bind('<Any-KeyPress>', lambda e: root.destroy())
root.mainloop()

这在 Windows 操作系统上运行良好。然而,这在 Ubuntu 上不起作用,除非我删除行 root.overrideredirect(True)从上面的代码来看。

这是预期的行为吗?

或者有没有一种方法可以让我的程序在仍然使用 root.overrideredirect(True) 的情况下工作?

编辑

<小时/>

我刚刚看到一个similar question here at SO ,其中 Bryan Oakley 建议使用 root.focus_force()但这没有帮助。

编辑2

<小时/>

我用过root.attributes('-fullscreen', True)而不是root.overrideredirect(True)按照建议here现在看来这可行了。

最佳答案

试试这个:

from tkinter import *

root = Tk()

root.bind('<Any-KeyPress>', quit())
root.mainloop()

假设您希望程序退出,请保留代码。如果您只想清除屏幕,只需使用 root.destroy() 而不是 quit()。使用 root.overrideredirect(True) 在 Ubuntu 上不起作用。

关于python - root.overrideredirect 和 <Any-KeyPress> 绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32467924/

相关文章:

python - wxPython:手动调用事件

c# - 犀牛模拟。如何添加订阅事件处理程序的期望

python - 将值插入文本小部件并删除先前的值 - Tkinter

Python:循环和字符串

JavaScript 类型错误 : var is not a function

python - 在文件中查找单词

python - 如何将 Tkinter 安装到 Aptana Studio 35-32(非 Eclipse)

python - 如何将两个后续事件绑定(bind)到 python 中的一个回调?

python - mysqldb 数据库主机未被接受?

python - 如何在我的 Cloud-Run Flask 应用程序中获取用户的 IP 地址?