python-3.x - Python 3 Tkinter 无边界全屏应用程序

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

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

我希望应用程序占据整个屏幕,而窗口管理器的标题栏和框架不在应用程序周围,通常在游戏中被称为全屏无边框窗口。

我试过使用 root.attributes("-fullscreen", True)root.overrideredirect(True)root.wm_attributes("-topmost", 1) .然而,包含 root.overrideredirect(True)行不允许它正常全屏;它仍然显示 Mac Dock 和任务栏,并且还破坏了我在应用程序中的按键绑定(bind)。没有 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 问题。

最佳答案

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

您想摆脱窗口管理器栏吗?看看the docs它声明了一个使用 -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() - 实例。所以我不需要覆盖 parent ,而只需要“我自己”来谈论类(class)的 POV。

关于python-3.x - Python 3 Tkinter 无边界全屏应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31026189/

相关文章:

python - 如何允许 Tkinter 从列表输入生成列表框

python - 为什么我从另一个类继承时 __init__() 必须使用两次?

Python 3.x - 在 tkinter 中切换全屏

javascript - 我们如何移动 Electron 应用程序以在第二/外部屏幕上显示?

java - 如何在 Controller 类中引用 Stage?

python re,多个匹配组

python - time .sleep() 在命令中以错误的顺序发生;总是在函数的开头

Python包安装错误-找不到py_compiler msvc

python - 如何将 TKinter 中的 .configure 压缩为一个命令?

合并两个重叠列表的Pythonic方法,保留顺序