pythontkinterscreenfullscreen

How to make a window fullscreen in a secondary display with tkinter?


I know how to make a window fullscreen in the "main" display, but even when moving my app's window to a secondary display connected to my PC, when I call:

self.master.attributes('-fullscreen', True)

to fullscreen that window, it does so in the "main" display and not in the secondary one (the app's window disappears from the secondary display and instantly appears in the "main" one, in fullscreen).

How can I make it fullscreen in the secondary display?


Solution

  • This works on Windows 7: If the second screen width and height are the same as the first one, you can use win1 or win2 geometry in the following code depending on its relative position (leftof or rightof) to have a fullscreen window on a secondary display:

    from tkinter import *
    
    def create_win():
        def close():
            win1.destroy()
            win2.destroy()
    
        win1 = Toplevel()
        win1.geometry('%dx%d%+d+%d'%(sw, sh, -sw, 0))
        Button(win1, text="Exit1", command=close).pack()
    
        win2 = Toplevel()
        win2.geometry('%dx%d%+d+%d'%(sw, sh, sw, 0))
        Button(win2, text="Exit2", command=close).pack()
    
    root = Tk()
    sw, sh = root.winfo_screenwidth(), root.winfo_screenheight()
    print(f"screen1:\nscreenwidth: {sw}\nscreenheight: {sh}")
    
    w, h = 800, 600
    a, b = (sw-w)/2, (sh-h)/2
    
    Button(root, text="Exit", command=lambda r=root:r.destroy()).pack()
    Button(root, text="Create win2", command=create_win).pack()
    
    root.geometry('%sx%s+%s+%s'%(w, h, a, b))
    root.mainloop()