我一直在为参加考试创建一个应用程序。为此,我必须做两件事。首先,禁用Tkinter窗口的拖动,不要让用户关注其他窗口,而不是我的应用程序窗口。这意味着我希望使我的应用程序能够在我的应用程序使用时不能使用其他应用程序。
发布于 2021-04-17 09:14:34
试试这个:
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和tkinter.Button
来创建自己的标题栏。我试着让它与标题栏一起工作,但由于某些原因,我无法重新调整窗口的焦点。
发布于 2021-04-17 09:47:09
这样做的一种方法是通过以下方法,另一种可能是覆盖tkinter的.geometry()
方法。
在下面的代码中,我只是通过使用winfo_rootx
和winfo_rooty
获得了这个位置。在此之后,您可以通过每次配置窗口时绑定事件来调用几何学方法来强制窗口。
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()
https://stackoverflow.com/questions/67136261
复制相似问题