Search code examples
pythonwindowstkinterpython-imaging-libraryfullscreen

Python Code to Display fullscreen in main object (tkinter and PIL)


With this code I can display an Image in full screen mode.

from tkinter import *
from PIL import Image, ImageTk


def showPIL(pilImage):
    root = Tk()
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    root.overrideredirect(1)
    root.geometry("%dx%d+0+0" % (w, h))
    root.focus_set()
    root.bind("<Escape>", lambda e: (e.widget.withdraw(), e.widget.destroy()))
    canvas = Canvas(root, width=w, height=h)
    canvas.pack()
    canvas.configure(background='black')
    imgWidth, imgHeight = pilImage.size
    if imgWidth > w or imgHeight > h:
        ratio = min(w / imgWidth, h / imgHeight)
        imgWidth = int(imgWidth * ratio)
        imgHeight = int(imgHeight * ratio)
        pilImage = pilImage.resize((imgWidth, imgHeight), Image.ANTIALIAS)
    image = ImageTk.PhotoImage(pilImage)
    imagesprite = canvas.create_image(w / 2, h / 2, image=image)
    root.mainloop()


img1 = Image.open("img1.png")
img2 = Image.open('img2.png')

while True:
    n = int(input('Numero: '))
    if n == 1:
        showPIL(img1)
        continue
    elif n == 2:
        showPIL(img2)
        continue
    else:
        break

But I have a problem... The image that opens does not come as the main object on the screen, sometimes it is behind some open window or program ... How do I leave as the main screen?


Solution

  • You can put this root.attributes("-topmost", True) under that root.bind. @StevoMitric

    that solved my question.