如何在 Tkinter 中创建全屏窗口

Jinku Hu 2023年1月30日 2019年12月10日
  1. Windows root.attributes('-fullscreen', True) 在 Tkinter 中创建全屏模式
  2. 在 Tkinter 中 Ubuntu root.attributes('-zoomed', True) 创建全屏模式
  3. 显示带有工具栏的全屏模式
如何在 Tkinter 中创建全屏窗口

在本教程中,我们将介绍如何在 Tkinter 中创建全屏窗口,以及如何切换或退出全屏模式。

Windows root.attributes('-fullscreen', True) 在 Tkinter 中创建全屏模式

tk.Tk.attributes 设置特定操作系统平台的属性。Windows 中的属性是

  • -alpha
  • -transparentcolor
  • -disabled
  • -fullscreen
  • -toolwindow
  • -topmost

-fullscreen 指定窗口是否为全屏模式。

import tkinter as tk

class Fullscreen_Example:
    def __init__(self):
        self.window = tk.Tk()
        self.window.attributes('-fullscreen', True)  
        self.fullScreenState = False
        self.window.bind("<F11>", self.toggleFullScreen)
        self.window.bind("<Escape>", self.quitFullScreen)

        self.window.mainloop()

    def toggleFullScreen(self, event):
        self.fullScreenState = not self.fullScreenState
        self.window.attributes("-fullscreen", self.fullScreenState)

    def quitFullScreen(self, event):
        self.fullScreenState = False
        self.window.attributes("-fullscreen", self.fullScreenState)

if __name__ == '__main__':
    app = Fullscreen_Example()
self.window.bind("<F11>", self.toggleFullScreen)

绑定 F11toggleFullScreen 函数。

def toggleFullScreen(self, event):
        self.fullScreenState = not self.fullScreenState
        self.window.attributes("-fullscreen", self.fullScreenState)

在此函数中,fullscreen 模式被更新为跟前一状态相反的状态。

def quitFullScreen(self, event):
        self.fullScreenState = False
        self.window.attributes("-fullscreen", self.fullScreenState)

quitFullScreen 函数通过设置 -fullscreenFalse 而退出全屏模式。

我们可以使用 lambda 函数来简化解决方案。

import tkinter as tk

class Fullscreen_Example:
    def __init__(self):
        self.window = tk.Tk()
        self.window.attributes('-fullscreen', True)  
        self.window.bind("<F11>",
                         lambda event: self.window.attributes("-fullscreen",
                                    not self.window.attributes("-fullscreen")))
        self.window.bind("<Escape>",
                         lambda event: self.window.attributes("-fullscreen",
                                    False))

        self.window.mainloop()

if __name__ == '__main__':
    app = Fullscreen_Example()
self.window.bind("<F11>",
     lambda event: self.window.attributes("-fullscreen",
                not self.window.attributes("-fullscreen")))

它将 lambda 函数绑定到 F11,可以在其中读取当前的全屏状态 self.window.attributes("-fullscreen"),如果该方法中未指定任何值,则该方法返回 -fullscreen 的当前状态。

如果遵循这种方法,我们就不再需要 fullScreenState 状态变量。

在 Tkinter 中 Ubuntu root.attributes('-zoomed', True) 创建全屏模式

-fullscreen 属性仅在 Windows 上存在,而在 Linux 或 macOS 上并不存在。Ubuntu 具有类似的属性 -zoomed,可将窗口设置为全屏模式。

import tkinter as tk

class Ubuntu_Fullscreen_Example:
    def __init__(self):
        self.window = tk.Tk()
        self.window.attributes('-zoomed', True)  
        self.fullScreenState = False
        self.window.bind("<F11>", self.toggleFullScreen)
        self.window.bind("<Escape>", self.quitFullScreen)

        self.window.mainloop()

    def toggleFullScreen(self, event):
        self.fullScreenState = not self.fullScreenState
        self.window.attributes("-zoomed", self.fullScreenState)

    def quitFullScreen(self, event):
        self.fullScreenState = False
        self.window.attributes("-zoomed", self.fullScreenState)

if __name__ == '__main__':
    app = Ubuntu_Fullscreen_Example()

显示带有工具栏的全屏模式

上面代码中显示的全屏模式中,工具栏是不可见的。如果需要在窗口中显示工具栏,则窗口的几何形状应与显示器的尺寸相同。

import tkinter as tk

class Fullscreen_Example:
    def __init__(self):
        self.window = tk.Tk()
        self.fullScreenState = False
        self.window.attributes("-fullscreen", self.fullScreenState)

        self.w, self.h = self.window.winfo_screenwidth(), self.window.winfo_screenheight()
        self.window.geometry("%dx%d" % (self.w, self.h))
        
        self.window.bind("<F11>", self.toggleFullScreen)
        self.window.bind("<Escape>", self.quitFullScreen)

        self.window.mainloop()

    def toggleFullScreen(self, event):
        self.fullScreenState = not self.fullScreenState
        self.window.attributes("-fullscreen", self.fullScreenState)

    def quitFullScreen(self, event):
        self.fullScreenState = False
        self.window.attributes("-fullscreen", self.fullScreenState)

if __name__ == '__main__':
    app = Fullscreen_Example()
self.w, self.h = self.window.winfo_screenwidth(), self.window.winfo_screenheight()

winfo_screenwidth()winfo_screenheight() 获得显示器的宽度和高度。

self.window.geometry("%dx%d" % (self.w, self.h))

使用 geometry 方法将 GUI 窗口大小设置为与显示器的宽度和高度相同。

Author: Jinku Hu
Jinku Hu avatar Jinku Hu avatar

Founder of DelftStack.com. Jinku has worked in the robotics and automotive industries for over 8 years. He sharpened his coding skills when he needed to do the automatic testing, data collection from remote servers and report creation from the endurance test. He is from an electrical/electronics engineering background but has expanded his interest to embedded electronics, embedded programming and front-/back-end programming.

LinkedIn

相关文章 - Tkinter Geometry