使用 tkinter 无法关闭 window
Unclosable window using tkinter
嘿,我正在制作一个程序,当我输入错误的密码时,它会使用我的网络摄像头拍照。该程序将打开,我希望它不可关闭。
我需要知道如何使用 tkinter 使 window 不可关闭。
Tkinter 没有任何方法可以直接执行此操作。但它确实有一些东西 可能 足够好,或者它可能太过分了:overrideredirect
标志:
If non-zero, this prevents the window manager from decorating the window. In other words, the window will not have a title or a border, and it cannot be moved or closed via ordinary means.
这还不够up-to-date;它在某些平台上可能实际上有标题或边框……但它不会关闭。
这很容易使用:只需执行 root.overrideredirect(True)
(或者,如果您想对不同的 Toplevel
window 而不是您的根执行此操作,window.overrideredirect(True)
).
但请注意,它不能移动或关闭,而不仅仅是它不能关闭。 (如果需要,它也无法调整大小。)
因此,您唯一能做的就是设置标志,然后绑定 mouse-button 事件以手动处理移动。例如,在您的 window 的 __init__
方法中:
self.overrideredirect(True) # if this is a Toplevel
#self.parent.overrideredirect(True) # if this is a Frame on root
self.bind('<ButtonPress-1>', self.move_start)
self.bind('<ButtonRelease-1>', self.move_end)
self.bind('<B1-Motion>', self.move_move)
然后:
def move_start(self, event):
self.startx, self.starty = event.x, event.y
def move_stop(self, event):
self.move_move(event)
def move_move(self, event):
x = self.winfo_x() + event.x - self.startx
y = self.winfo_y() + event.y - self.starty
self.geometry("+%s+%s" % (x, y))
显然,如果您希望 window 中的任何小部件接受点击,您可能不希望将整个 window 设为拖动区域。事实上,您可能不想将整个 window 设为拖动区域,即使它没有任何可单击的内容,因为这并没有真正遵循 Mac 或 Windows 人机界面指南。您可以伪造一个抓握区域——标题栏、window 周围的边框等——只需添加一个 Label
固定到您想要抓握的一侧,并且仅bind
ing 在那里,或者通过从主 window 中创建一个 "child" window 插入来窃取 bind
ings。但这永远不会看起来像 "native" window.
如果您确实需要原生 window,但要使用关闭框([=53= 上 top-right 角中的 X
,[=53= 中的红点=51=] 在 Mac 等上)被禁用或丢失(并且在 Windows 上 window 菜单上的 "close" 项目被禁用,Alt+F4,等等,对于 X11 也是如此……)……据我所知,在 Tkinter 中没有 cross-platform 方法可以做到这一点。您将必须为每个平台编写代码,以获取底层原生 window objects 并对它们执行原生 window 操作。在这一点上,你可能想看看使用比 Tkinter 更强大的 windowing 库——例如,我相信 Qt、Gtk+ 和 wx 都有更简单的方法来创建一个普通的 window 但关闭框已禁用。
您可以尝试@abarnert 建议的所有方法,但我认为最简单的方法是忽略关闭事件。
来自this question:
Here you have a concrete example:
import Tkinter as tk
import tkMessageBox as messagebox
root = tk.Tk()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
(Windows 的编辑代码)
所以将on_closing()
改为
def on_closing():
pass
这使得它无法关闭。我尝试了 Alt+F4,关闭按钮,从 Windows 任务栏关闭它,都无济于事。我能够杀死它的唯一方法是使用任务管理器。
嘿,我正在制作一个程序,当我输入错误的密码时,它会使用我的网络摄像头拍照。该程序将打开,我希望它不可关闭。 我需要知道如何使用 tkinter 使 window 不可关闭。
Tkinter 没有任何方法可以直接执行此操作。但它确实有一些东西 可能 足够好,或者它可能太过分了:overrideredirect
标志:
If non-zero, this prevents the window manager from decorating the window. In other words, the window will not have a title or a border, and it cannot be moved or closed via ordinary means.
这还不够up-to-date;它在某些平台上可能实际上有标题或边框……但它不会关闭。
这很容易使用:只需执行 root.overrideredirect(True)
(或者,如果您想对不同的 Toplevel
window 而不是您的根执行此操作,window.overrideredirect(True)
).
但请注意,它不能移动或关闭,而不仅仅是它不能关闭。 (如果需要,它也无法调整大小。)
因此,您唯一能做的就是设置标志,然后绑定 mouse-button 事件以手动处理移动。例如,在您的 window 的 __init__
方法中:
self.overrideredirect(True) # if this is a Toplevel
#self.parent.overrideredirect(True) # if this is a Frame on root
self.bind('<ButtonPress-1>', self.move_start)
self.bind('<ButtonRelease-1>', self.move_end)
self.bind('<B1-Motion>', self.move_move)
然后:
def move_start(self, event):
self.startx, self.starty = event.x, event.y
def move_stop(self, event):
self.move_move(event)
def move_move(self, event):
x = self.winfo_x() + event.x - self.startx
y = self.winfo_y() + event.y - self.starty
self.geometry("+%s+%s" % (x, y))
显然,如果您希望 window 中的任何小部件接受点击,您可能不希望将整个 window 设为拖动区域。事实上,您可能不想将整个 window 设为拖动区域,即使它没有任何可单击的内容,因为这并没有真正遵循 Mac 或 Windows 人机界面指南。您可以伪造一个抓握区域——标题栏、window 周围的边框等——只需添加一个 Label
固定到您想要抓握的一侧,并且仅bind
ing 在那里,或者通过从主 window 中创建一个 "child" window 插入来窃取 bind
ings。但这永远不会看起来像 "native" window.
如果您确实需要原生 window,但要使用关闭框([=53= 上 top-right 角中的 X
,[=53= 中的红点=51=] 在 Mac 等上)被禁用或丢失(并且在 Windows 上 window 菜单上的 "close" 项目被禁用,Alt+F4,等等,对于 X11 也是如此……)……据我所知,在 Tkinter 中没有 cross-platform 方法可以做到这一点。您将必须为每个平台编写代码,以获取底层原生 window objects 并对它们执行原生 window 操作。在这一点上,你可能想看看使用比 Tkinter 更强大的 windowing 库——例如,我相信 Qt、Gtk+ 和 wx 都有更简单的方法来创建一个普通的 window 但关闭框已禁用。
您可以尝试@abarnert 建议的所有方法,但我认为最简单的方法是忽略关闭事件。
来自this question:
Here you have a concrete example:
import Tkinter as tk
import tkMessageBox as messagebox
root = tk.Tk()
def on_closing():
if messagebox.askokcancel("Quit", "Do you want to quit?"):
root.destroy()
root.protocol("WM_DELETE_WINDOW", on_closing)
root.mainloop()
(Windows 的编辑代码)
所以将on_closing()
改为
def on_closing():
pass
这使得它无法关闭。我尝试了 Alt+F4,关闭按钮,从 Windows 任务栏关闭它,都无济于事。我能够杀死它的唯一方法是使用任务管理器。