Python 3 Tkinter 无边框全屏应用程序

15

我在创建Python 3 tkinter应用程序时遇到了问题。我目前正在使用Mac OSX系统进行开发,但我通常使用Windows操作系统。

我希望该应用程序占据整个屏幕,而无需窗口管理器的标题栏和框架环绕应用程序,通常被称为全屏无边框窗口(游戏中经常使用)。

我尝试使用root.attributes("-fullscreen", True)root.overrideredirect(True)root.wm_attributes("-topmost", 1)。然而,包含root.overrideredirect(True)行不允许它进入真正的全屏模式; 它仍然显示Mac Dock和任务栏,并且它还会破坏应用程序中的键绑定。如果没有root.overrideredirect(True)行,则应用程序会进入全屏模式(隐藏Dock和任务栏),但窗口不会填满整个屏幕; 它在底部留下一个间隙,而且还保留窗口管理器的标题栏和框架/边框。

以下是我的代码示例:

import tkinter as tk

class App(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent)

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Fullscreen Application")

        self.pack(fill="both", expand=True, side="top")

        self.parent.wm_state("zoomed")

        self.parent.bind("<F11>", self.fullscreen_toggle)
        self.parent.bind("<Escape>", self.fullscreen_cancel)

        self.fullscreen_toggle()

        self.label = tk.Label(self, text="Fullscreen", font=("default",120), fg="black")
        self.label.pack(side="top", fill="both", expand=True)

    def fullscreen_toggle(self, event="none"):

        self.parent.focus_set()
        self.parent.overrideredirect(True)

        self.parent.attributes("-fullscreen", True)
        self.parent.wm_attributes("-topmost", 1)

    def fullscreen_cancel(self, event="none"):

        self.parent.overrideredirect(False)
        self.parent.attributes("-fullscreen", False)
        self.parent.wm_attributes("-topmost", 0)

        self.centerWindow()

    def centerWindow(self):

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        w = sw*0.7
        h = sh*0.7

        x = (sw-w)/2
        y = (sh-h)/2

        self.parent.geometry("%dx%d+%d+%d" % (w, h, x, y))

if __name__ == "__main__":
    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

我希望有人能够帮助!谢谢!

编辑:我刚在Windows电脑上测试了一下。没有 self.parent.overrideredirect(True),它会创建应用程序,并完美地按照期望工作(全屏但没有窗口管理器边框或标题栏)。这必须只是OSX的问题。


1
你尝试过改变根几何图形,将宽度和/或高度增加5或其他值吗? - Dan Alexander
我试过了,可惜没成功。不管怎样,还是谢谢你的建议。 :) - jacobian
1
请选择一个答案,或者评论那些没有满足所需修复的答案。 - user4734394
2个回答

1
#!/usr/bin/python
import Tkinter as tk

class App(tk.Frame):
    def __init__(self, parent, *args, **kwargs):
        tk.Frame.__init__(self, parent)

        self.parent = parent

        self.initUI()

    def initUI(self):

        self.parent.title("Fullscreen Application")

        self.pack(fill="both", expand=True, side="top")

        self.parent.wm_state("zoomed")

        self.parent.bind("<F11>", self.fullscreen_toggle)
        self.parent.bind("<Escape>", self.fullscreen_cancel)

        self.fullscreen_toggle()

        self.label = tk.Label(self, text="Fullscreen", font=("default",120), fg="black")
        self.label.pack(side="top", fill="both", expand=True)

    def fullscreen_toggle(self, event="none"):

        self.parent.focus_set()
        self.parent.overrideredirect(True)
        self.parent.overrideredirect(False) #added for a toggle effect, not fully sure why it's like this on Mac OS
        self.parent.attributes("-fullscreen", True)
        self.parent.wm_attributes("-topmost", 1)

    def fullscreen_cancel(self, event="none"):

        self.parent.overrideredirect(False)
        self.parent.attributes("-fullscreen", False)
        self.parent.wm_attributes("-topmost", 0)

        self.centerWindow()

    def centerWindow(self):

        sw = self.parent.winfo_screenwidth()
        sh = self.parent.winfo_screenheight()

        w = sw*0.7
        h = sh*0.7

        x = (sw-w)/2
        y = (sh-h)/2

        self.parent.geometry("%dx%d+%d+%d" % (w, h, x, y))

if __name__ == "__main__":
    root = tk.Tk()
    App(root).pack(side="top", fill="both", expand=True)
    root.mainloop()

2
如果你想更具体一些,可以点击链接 - Gumboy

1
为了解决您的OS-X问题,我将提供一种解决方案,它可以为我解决类似的问题(在Linux和Windows之间使用全屏时出现了一些问题)。
您想要摆脱窗口管理器栏吗?请查看文档,它说明了一个选项,通过使用-toolwindow选项来移除窗口管理器项目。
关于您的应用程序大小,以下是我在Linux上使用的帮助 - “手动缩放”:
class MyClass(tk.Tk):
    def __init__(self, *args, **kwargs):
        tk.Tk.__init__(self, *args, **kwargs)
        self.overrideredirect(True) # depending on your needs
        self.attributes("-toolwindow", 1) # this line removes the window managers bar

        try:                                   # Automatic zoom if possible
            self.wm_state("zoomed")
            print("Using automatic zoom")
        except tk.TclError:                    # Manual zoom
            # Bad Argument Error is a TclError
            print("Using manual zoom")

            # get the screen dimensions
            width = self.winfo_screenwidth()
            height = self.winfo_screenheight()

            # build a geometry string. 
            # form: width x height + x_offset + y_offset
            geom_string = "%dx%d+0+0" % (width, height)
            self.wm_geometry(geom_string)

请注意,我在这里没有使用未配置的tk.Tk()实例 - 我的类就是tk.Tk()实例。因此,我不需要覆盖父级,只需要从类的角度来说“覆盖”自己即可。

工具窗口没有图标、最小化和最大化按钮的标题栏。它只有一个标题和一个关闭按钮。这不是一个好的解决方案。 - JP_

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