首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何禁用Tkinter窗口的移动而不移除标题栏

如何禁用Tkinter窗口的移动而不移除标题栏
EN

Stack Overflow用户
提问于 2021-04-17 08:39:50
回答 2查看 326关注 0票数 2

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-04-17 09:14:34

试试这个:

代码语言:javascript
运行
复制
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.Labels和tkinter.Button来创建自己的标题栏。我试着让它与标题栏一起工作,但由于某些原因,我无法重新调整窗口的焦点。

票数 1
EN

Stack Overflow用户

发布于 2021-04-17 09:47:09

这样做的一种方法是通过以下方法,另一种可能是覆盖tkinter的.geometry()方法。

在下面的代码中,我只是通过使用winfo_rootxwinfo_rooty获得了这个位置。在此之后,您可以通过每次配置窗口时绑定事件来调用几何学方法来强制窗口。

代码语言:javascript
运行
复制
import tkinter as tk

def get_pos():
    global x,y
    x = root.winfo_rootx()
    y = root.winfo_rooty()
def fix_pos():
    root.bind('<Configure>', stay_at)
def stay_at(event):
    root.geometry('+%s+%s' % (x,y))

root = tk.Tk()
button1 = tk.Button(root, text='get_pos', command=get_pos)
button2 = tk.Button(root, text='fix_pos', command=fix_pos)

button1.pack()
button2.pack()

root.mainloop()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/67136261

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档