How would I make a command check whether a window has the attributes of Fullscreen as True or False

Question:

So I am trying to make some simple buttons that allow the user to minimize and maximize the app window and I am trying to make it so a button can be pressed to set the window as fullscreen and when in fullscreen it can be pressed again to set the window in windowed mode. I have tried to set the window attributes for fullscreen as functions to make an if else statement but all that would happen is that the app would go fullscreen and if I press the button again it would only minimize for a second before going back to fullscreen.

import tkinter.messagebox
from tkinter import *
import customtkinter
from PIL import Image, ImageTk
import sys

customtkinter.set_appearance_mode("dark") # Modes: System(default), Light, Dark
customtkinter.set_default_color_theme("dark-blue") #modes: blue, dark-blue, green

window = customtkinter.CTk()
window.title("To-Do List")
#window.overrideredirect(1)

#fullscreen
def Fulwin():
    window.attributes('-fullscreen', True)
#windowed mode
def Nonwin():
    window.attributes('-fullscreen', False)


## Minimize, maximize, and exit button

def Ifullscreen():
    if Fulwin:
        Nonwin
    else: Fulwin

#Maxamize/Minimize Button

maximize_image = ImageTk.PhotoImage(Image.open("GUI/Maximize.png"))
maximize_button = customtkinter.CTkButton(master=window, image=maximize_image, text="",command=Ifullscreen,
                                          width=30, height=30, fg_color='#1a1a1a', hover_color='#1a1a1a')
maximize_button.pack(padx=5,pady=5)
maximize_button.place(x=1450,y=10)

#Hide Button
def Minsize():
    window.attributes('-fullscreen',False)
minimize_image = ImageTk.PhotoImage(Image.open("GUI/minimize.png"))
minimize_button = customtkinter.CTkButton(master=window, image=minimize_image, text="", command=Minsize,
                                          width=30, height=30, fg_color='#1a1a1a', hover_color='#1a1a1a')
minimize_button.pack(padx=5,pady=5)
minimize_button.place(x=1400,y=10)

#Exit Button
def Exsize():
    sys.exit()
exit_image = ImageTk.PhotoImage(Image.open("GUI/exit.png"))
exit_button = customtkinter.CTkButton(master=window, image=exit_image, text="", command=Exsize,
                                      width=30, height=30, fg_color='#1a1a1a', hover_color='#1a1a1a')
exit_button.pack(padx=5,pady=5)
exit_button.place(x=1500,y=10)

window.mainloop()
Asked By: Suchi

||

Answers:

Side note: For future questions could you make what part you are having problems with more explicit (e.g. Putting a comment before what you are asking and keeping your code snippets to a small size).

Side note 2: With comments please stick to 1 commenting style throughout your code.

The Nonwin and Fulwin functions can just be replaced to one function: e.g.

def Ifullscreen():
    """ Minimize, maximize, and exit button """
    window.attributes("-fullscreen", not window.attributes("-fullscreen"))

And then just delete Fulwin and Nonwin functions.

If you wish to keep similar function names (I would not recommend this) here you go:


def Fulwin():
    """ Fullscreen mode """
    window.attributes('-fullscreen', True)

def Nonwin():
    """ Windowed mode """
    window.attributes('-fullscreen', False)

def Ifullscreen():
    """ Minimize, maximize, and exit button """
    if window.attributes('-fullscreen'):
        Nonwin()
    else:
        Fulwin()

I’m pretty sure this is what you were looking for.

Answered By: xihtyM
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.