How to make Tkinter window in full screen while starting

To make a Tkinter window start in full screen mode, you can use the attributes method of the Tk class to set the window to full screen. Here’s a step-by-step guide:

  1. Import Tkinter: Start by importing the Tkinter module.
  2. Create the main window: Instantiate the Tk class.
  3. Set the window to full screen: Use the attributes method with the -fullscreen option.
  4. Add any widgets: You can add widgets to the window as usual.
  5. Start the Tkinter event loop: Call the mainloop method to run the application.
    import tkinter as tk
    
    def main():
        # Create the main window
        root = tk.Tk()
        
        # Set the window to full screen
        root.attributes('-fullscreen', True)
        
        # Optionally, you can set the window to cover only the primary monitor
        # root.attributes('-zoomed', True)  # This works for maximizing, not full-screen
        
        # Add a label to the window
        label = tk.Label(root, text="Full Screen Mode", font=('Arial', 24))
        label.pack(expand=True)
        
        # Add an exit button to exit full screen
        def exit_fullscreen(event=None):
            root.attributes('-fullscreen', False)
        
        # Bind the Escape key to exit full screen mode
        root.bind("<Escape>", exit_fullscreen)
        
        # Start the Tkinter event loop
        root.mainloop()
    
    if __name__ == "__main__":
        main()

    Explanation:

    1. root.attributes('-fullscreen', True): This line makes the window go full screen.
    2. exit_fullscreen function: This function allows you to exit full-screen mode by pressing the Escape key. You can bind this function to the Escape key with root.bind("<Escape>", exit_fullscreen).
    3. label.pack(expand=True): The label is centered and expands to fill the available space.

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top