简体   繁体   中英

I need help displaying full screen images with tkinter

I am doing a people counter in raspberry pi. I want to display an one image if someone comes in, and another one if someone comes out. Right now i am using the code below (that i took from another question here xd) to change the image that tkinter is displaying. The problem with this is thay it only shows the picture cat.jpg for a second, and then it shows a black screen and nothing happends.

import sys
if sys.version_info[0] == 2:  # the tkinter library changed it's name from Python 2 to 3.
    import Tkinter
    tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
    import tkinter
from PIL import Image, ImageTk
import time

def updateRoot(root,imagen):  
    pilImage = Image.open(imagen)
    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.quit()))
    canvas = tkinter.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.update()

root = tkinter.Tk()

updateRoot(root,"Cat.jpg")

time.timesleep(5)

updateRoot(root,"Dog.jpg")

Before this I used this code

import tkinter
from PIL import Image, ImageTk

from tkinter import ttk

def updateRoot(root,imagen):

    image1 = Image.open(imagen)
    image2 =  ImageTk. PhotoImage(image1)
    image_label = ttk. Label(root , image =image2)
    image_label.place(x = 0 , y = 0)
    root.update()

That works fine, but it's not full screen.

First you should do the followings outside updateRoot() :

  • make root window fullscreen (you can simply use root.attributes('-fullscreen', 1) )
  • bind the <Escape> key
  • create the canvas and create_image() (you can use Label to do the same thing)

Then just update the image inside updateRoot() .

Also you should use after() instead of time.sleep() .

Below is an example:

try:
    import Tkinter as tkinter
except:
    import tkinter
from PIL import Image, ImageTk

def updateRoot(imagen):
    # resize the image to fill the whole screen
    pilImage = Image.open(imagen)
    w, h = root.winfo_screenwidth(), root.winfo_screenheight()
    image = ImageTk.PhotoImage(pilImage.resize((w,h)))
    # update the image
    canvas.itemconfig(imgbox, image=image)
    # need to keep a reference of the image, otherwise it will be garbage collected
    canvas.image = image

root = tkinter.Tk()
root.attributes('-fullscreen', 1)
root.bind('<Escape>', lambda _: root.destroy())

canvas = tkinter.Canvas(root, highlightthickness=0)
canvas.pack(fill=tkinter.BOTH, expand=1)
imgbox = canvas.create_image(0, 0, image=None, anchor='nw')

# show the first image
updateRoot('Cat.jpg')
# change the image 5 seconds later
root.after(5000, updateRoot, 'Dog.jpg')

root.mainloop()

Fixed your Black issue using labels, try this. i think you still need to resize image to fit screen

import sys
if sys.version_info[0] == 2:  # the tkinter library changed it's name from Python 2 to 3.
    import Tkinter
    tkinter = Tkinter #I decided to use a library reference to avoid potential naming conflicts with people's programs.
else:
    import tkinter

from PIL import Image, ImageTk
import time
from tkinter import *
import PIL.Image

def updateRoot(root,imagen):  
    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.quit()))
    img = PIL.Image.open(imagen)
    root.tkimage = ImageTk.PhotoImage(img)
    Label(root,image = root.tkimage).place(x=0, y=0, relwidth=1, relheight=1)
    root.update()

root = tkinter.Tk()
updateRoot(root,"Cat.jpg")
time.sleep(3)
updateRoot(root,"Dog.jpg")
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM