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

BCL*_*Ltd 3 python macos tkinter fullscreen python-2.7

我刚刚学习Python,我正在尝试让一个窗口全屏显示,我已经实现了,但我现在想去掉顶部的标题栏。它目前看起来如下图所示,但我希望它也能覆盖顶部的 Mac 顶部工具栏(如启动屏幕)。

在此输入图像描述

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()
Run Code Online (Sandbox Code Playgroud)

Mik*_*SMT 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()
Run Code Online (Sandbox Code Playgroud)

如果由于 MacOS 的原因这不起作用,请查看此链接。这个有用的页面提供了有关如何在 tkinter 中管理 mack 窗口的多个示例。我相信您可能需要获得无边框全屏。

这段代码可能正是您所需要的:

root.tk.call("::tk::unsupported::MacWindowStyle", "style", root._w, "plain", "none")
Run Code Online (Sandbox Code Playgroud)

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

更新:

还有另一段适用于 tkinter 8.5+ 的代码。

如果您将 python 与 tkinter 8.5 或更高版本一起使用:

root.wm_attributes('-fullscreen', 1)
Run Code Online (Sandbox Code Playgroud)