Buğra Coşkun
Buğra Coşkun

Reputation: 171

How to check if window is on Fullscreen in Tkinter?

I made F11 toggle fullscreen on. But how can I make it so that F11 can both toggle fullscreen on and off ?

I tried to make an [if] statement so it will turn it off if the window was previously toggled to fullscreen but I couldn't find a way to check if the window was already toggled or not.

Any help is appreciated, thank you.

Updated Solution :This is the final code that seems to work without a problem.

def toggle_fullscreen(event):
if (root.attributes('-fullscreen')):
    root.attributes('-fullscreen', False)

else:
    root.attributes('-fullscreen', True)
root.bind("<F11>", toggle_fullscreen)

Upvotes: 1

Views: 1919

Answers (3)

Kyle
Kyle

Reputation: 1

You can just write:

root.attributes("-fullscreen", not root.attributes('-fullscreen'))

It sets -fullscreen to whatever it isn't

Upvotes: 0

Bryan Oakley
Bryan Oakley

Reputation: 385960

root.attributes can be called with only a single argument to get the value of that argument.

if root.attribute('-fullscreen'):
    ...
else
    ...

Upvotes: 1

Parviz Karimli
Parviz Karimli

Reputation: 1287

This is the method I mentioned in my comment above:

from tkinter import *
root = Tk()

root.focus_set()

var = 0

def f(event):
    global var
    if var == 0:
        root.attributes("-fullscreen", True)
        var = 1
    else:
        root.attributes("-fullscreen", False)
        var = 0

root.bind("<F11>", f)

Upvotes: 2

Related Questions