无法在 tkinter 上退出全屏 window python

Cant exit fullscreen on tkinter window python

我正在尝试让这个 window 全屏显示,然后可以使用任意键退出。我无法将转义键绑定到实际转义 window,也无法从其他类似帖子中找出问题出在哪里。

代码如下:

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()

在某些系统上(如 Linux)root.overrideredirect(True) 删除 window 管理显示边框的 (WM),但 WM 还会将 key/mouse 事件从系统发送到您的程序。如果 WM 不发送事件,那么您的程序不知道您单击了 ESC。

这适用于 Linux Mint(基于 Ubuntu 和 Debian)。 Rasbian 基于 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 不起作用,5 秒后关闭它。

我使用 close_escape 只是为了查看它是否被 ESCafter() 关闭。如果代码有效,那么您可以使用 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()