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:
- Import Tkinter: Start by importing the Tkinter module.
- Create the main window: Instantiate the
Tkclass. - Set the window to full screen: Use the
attributesmethod with the-fullscreenoption. - Add any widgets: You can add widgets to the window as usual.
- Start the Tkinter event loop: Call the
mainloopmethod 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:
root.attributes('-fullscreen', True): This line makes the window go full screen.exit_fullscreenfunction: This function allows you to exit full-screen mode by pressing theEscapekey. You can bind this function to theEscapekey withroot.bind("<Escape>", exit_fullscreen).label.pack(expand=True): The label is centered and expands to fill the available space.