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

时间:2015-06-24 12:10:53

标签: python-3.x tkinter fullscreen window-managers

我在创建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)行,应用程序会进入全屏模式(隐藏停靠栏和任务栏),但窗口不会填满整个屏幕;它在底部留下了一个空隙,它还保留了窗口管理器的标题栏和框架/边框。

以下是我的代码示例:

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问题。

2 个答案:

答案 0 :(得分:0)

要修复您的OS-X问题,我将提供一个解决方案来解决类似的问题。 (有一些问题在Linux和Windows之间使用全屏)

你想摆脱Window Managers Bar?请查看the docs它指出了一个使用-toolwindow选项删除窗口管理器项的选项。

关于你的应用程序的大小,这有助于我使用linux - a&#34;手动缩放&#34;:

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() - 实例。所以我不需要覆盖父母,而只需要#34;我自己&#34;谈到班上的POV。

答案 1 :(得分:0)

#!/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()
相关问题