首页 > 解决方案 > 如何在不删除标题栏的情况下禁用 Tkinter 窗口的移动

问题描述

我一直在创建一个参加考试的应用程序。所以,为此,我必须做两件事。首先,禁用 Tkinter 窗口的拖动,不要让用户专注于其他窗口而不是我的应用程序窗口。这意味着我想让我的应用程序在我的应用程序正在使用时不能使用其他应用程序。

标签: pythonpython-3.xwindowstkinter

解决方案


尝试这个:

import tkinter as tk


class FocusedWindow(tk.Tk):
    def __init__(self, **kwargs):
        super().__init__(**kwargs)

        # Force it to be unminimisable
        super().overrideredirect(True)

        # Force it to always be on the top
        super().attributes("-topmost", True)

        # Even if the user unfoceses it, focus it
        super().bind("<FocusOut>", lambda event: self.focus_force())

        # Take over the whole screen
        width = super().winfo_screenwidth()
        height = super().winfo_screenheight()
        super().geometry("%ix%i+0+0" % (width, height))


root = FocusedWindow()
# You can use it as if it is a normal `tk.Tk()`
button = tk.Button(root, text="Exit", command=root.destroy)
button.pack()
root.mainloop()

tkinter.Label这删除了标题栏,但您始终可以使用s 和s创建自己的标题栏tkinter.Button。我尝试使它与标题栏一起使用,但由于某种原因我无法重新调整窗口的焦点。


推荐阅读