我试着把这个窗口变成全屏,然后可以用任何键逃跑。我无法将转义键绑定到实际逃离窗口,也无法从其他类似的帖子中找出它的错误所在。
以下是代码:
import tkinter as tk
from tkinter import *
root=Tk()
root.overrideredirect(True)
root.geometry("{0}x{1}+0+0".format(root.winfo_screenwidth(), root.winfo_screenheight()))
root.focus_set()
root.bind("<Escape>", lambda e: root.quit())
#mybuttonsandframesgohere
root.mainloop()
发布于 2017-11-27 11:46:21
在某些系统(如Linux)上,root.overrideredirect(True)
移除显示边框的窗口管理( WM ),但WM也将键/鼠标事件从系统发送到程序。如果WM不发送事件,那么您的程序不知道您单击了ESC。
这对我来说适用于Linux (基于Ubuntu和Debian)。拉辛是德比安的基地。
import tkinter as tk
def close_escape(event=None):
print("escaped")
root.destroy()
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1)
root.focus_set()
root.bind("<Escape>", close_escape)
root.after(5000, root.destroy) # close after 5s if `ESC` will not work
root.mainloop()
我只将root.after(5000, root.destroy)
放在测试中--如果ESC
不能工作,则在5s之后关闭它。
我使用close_escape
只是为了查看它是由ESC
还是after()
关闭的。如果代码有效,那么可以使用root.bind("<Escape>", lambda event:root.destroy())
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
root.overrideredirect(False)
root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1)
root.focus_set()
root.bind("<Escape>", lambda event:root.destroy())
root.mainloop()
顺便说一句:你也可以尝试不使用root.overrideredirect()
--也许它对你有用。
import tkinter as tk
root = tk.Tk()
root.attributes("-fullscreen", True)
root.wm_attributes("-topmost", 1) # keep on top
#root.focus_set()
root.bind("<Escape>", lambda event:root.destroy())
root.mainloop()
https://stackoverflow.com/questions/47509499
复制相似问题