tkinter在MacOS上始终将窗口置于首位 [英] tkinter keeping window on top all times on MacOS
问题描述
我正在尝试创建一个屏幕窗帘",该屏幕会阻塞除鼠标光标附近以外的屏幕的其他部分.在Windows上,即使我专注于另一个应用程序,使用 root.wm_attributes(-topmost","true")
仍可将窗口保持在顶部.但是,在MacOS上运行代码时,如果窗口焦点丢失了,它将无法保持其自身的最高位置.等同于 -topmost
窗口管理器属性的MacOS,它将始终保持窗口在顶部,而不管焦点如何?
I'm trying to create a screen "curtain" which blocks parts of the screen except for the mouse cursor's vicinity.
On windows, using root.wm_attributes("-topmost", "true")
keeps the window on top, even if I focus on another app, perfectly. However, upon running the code on MacOS, if the focus for the window is lost, it will not keep itself on topmost.
What would be the MacOS equivalent to -topmost
window manager attribute which will always keep the window on top, regardless of focus?
import tkinter as tk
class TransparentWindow(tk.Toplevel):
"""
This class is just a Toplevel window.
"""
def __init__(self, background="white", opacity=0.7):
super(TransparentWindow, self).__init__()
#self.master = master
self.configure(background=background)
self.overrideredirect(True)
self.wm_attributes("-alpha", opacity)
self.wm_attributes("-topmost", "true")
self.lift()
if __name__ == '__main__':
root = tk.Tk()
TransparentWindow()
root.mainloop()
在High Sierra虚拟机中运行此代码会导致在选择另一个窗口时,Toplevel不会一直处于顶部.
Running this code in a High Sierra Virtual Machine resulted in the Toplevel not constantly being on top when another window is selected.
推荐答案
在Mac OS上,使用 overrideredirect(True)
会禁用很多内容,例如 bind
,Button
按下和一些事件,老实说我不知道为什么.(如果有人知道,请发表评论).至少在Mac上,我有此问题,我已经阅读并看到并非所有Mac用户都遇到此问题.
On Mac OS using overrideredirect(True)
disables a lot of stuff like bind
, Button
presses and some events, honestly I don't know why exactly. (If anyone knows please comment). At least on my Mac I have this problem, I've read and seen that not all of Mac users have this problem.
所以这就是为什么 root.wm_attributes(-topmost","true")
无法正常工作的原因.但是不用担心我有解决方法.
So this is why root.wm_attributes("-topmost", "true")
is not working. But don't worry I got a workaround.
从您的代码中可以看出您想要一个 borderless 窗口,这是在所有绑定和事件仍能正常工作的情况下执行的操作.
From your code I can tell that you want a borderless window, here is how I do it with all bindings and event working still.
我首先将
overrideredirect(True)
放到下一行overrideredirect(False)
同样,在这种情况下,您不需要root.lift()
.
I first put
overrideredirect(True)
then in the next lineoverrideredirect(False)
Also you don't needroot.lift()
in this case.
确定,请尝试此代码,看看按钮是否正常按下.
Ok try this code and see if the button press normally.
样品
import tkinter as tk
root = tk.Tk()
root.overrideredirect(True)
# root.overrideredirect(False) # Uncomment and try again.
tk.Button(root, text="Borderless").pack()
root.wm_attributes("-topmost", "true")
root.wm_attributes("-alpha", 0.7)
root.wm_attributes("-topmost", "true")
# Doesn't matter if you use lift() or not with the use of root.overrideredirect(False) as well
root.lift()
root.mainloop()
希望这对您有所帮助.
以下是您的代码,该代码完全符合您的要求(至少在我的Mac上是这样).
Here is your code which worked exactly you want (At least on my Mac).
import tkinter as tk
class TransparentWindow(tk.Toplevel):
"""
This class is just a Toplevel window.
"""
def __init__(self, background="white", opacity=0.7):
super(TransparentWindow, self).__init__()
#self.master = master
self.configure(background=background)
self.overrideredirect(True)
self.overrideredirect(False)
self.wm_attributes("-alpha", opacity)
self.wm_attributes("-topmost", "true")
# self.lift()
if __name__ == '__main__':
root = tk.Tk()
TransparentWindow()
root.mainloop()
这篇关于tkinter在MacOS上始终将窗口置于首位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!