如何在macOS上全屏显示一个tkinter应用程序?

3
我正在学习Python,并尝试使窗口全屏,我已经做到了,但现在我想去掉顶部的标题栏。它目前看起来像下面的图片,但我希望它也覆盖Mac顶部的工具栏(就像启动画面一样)。

enter image description here

from tkinter import *
root = Tk()

root.attributes('-fullscreen', True)
root.attributes('-topmost', True)
root.overrideredirect(True)



def quitApp():
    # mlabel = Label (root, text = 'Close').pack()
    root.destroy()

# placing the button on my window
button = Button(text = 'QUIT', command = quitApp).pack()

有一个名为overrideredirect的方法可以在Windows上使用,但不确定它是否适用于Mac。 - Lafexlos
我尝试过了,但什么也没发生(可能是我做错了)。 - BCLtd
1个回答

5
我认为您想要做的是使用 root.wm_attributes('-fullscreen','true')。请尝试使用此方法,它应该可以解决问题。
from tkinter import *
root = Tk()

root.wm_attributes('-fullscreen','true')

def quitApp():
    root.destroy()

button = Button(text = 'QUIT', command = quitApp).pack()

root.mainloop()

如果这个方法因为MacOS系统无法实现,请查看此链接。这个有用的页面展示了如何在tkinter中管理mac窗口的几个例子,我相信你可能需要实现无边框全屏。
这段代码可能就是你所需的:
root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")

注意:如果您使用此选项,则需要从代码中删除root.wm_attributes('-fullscreen','true')或将其注释掉。

更新:

对于tkinter 8.5+,还有另一个代码片段。

如果您正在使用带有tkinter 8.5或更高版本的Python:

root.wm_attributes('-fullscreen', 1)

嗨,这将摆脱Mac工具栏(如果这就是它的名称),但仍然显示应用程序标题栏。 - BCLtd

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接