I am creating a software that when it receives the signal of a button, displays an image and after 1 second another one is displayed. The problem is that I do not know how to close the image once it has already been shown in full screen, it is assumed that the Esc closes, but it does not work.
import sys
import RPi.GPIO as GPIO
import time
pulse = 16
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(pulse, GPIO.IN, GPIO.PUD_UP)
if sys.version_info[0] == 2:
import Tkinter
tkinter = Tkinter
else:
import tkinter
from PIL import Image, ImageTk
blackImage = Image.open("black.png")
pattern = Image.open("pattern.jpg")
def showImage(nimage):
root = tkinter.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.quit()))
canvas = tkinter.Canvas(root,width=w,height=h)
canvas.pack()
canvas.configure(background='black')
imgWidth, imgHeight = nimage.size
if imgWidth > w or imgHeight > h:
ratio = min(w/imgWidth, h/imgHeight)
imgWidth = int(imgWidth*ratio)
imgHeight = int(imgHeight*ratio)
nimage = nimage.resize((imgWidth,imgHeight), Image.ANTIALIAS)
image = ImageTk.PhotoImage(nimage)
imagesprite = canvas.create_image(w/2,h/2,image=image)
root.mainloop()
while True:
if GPIO.input(pulse) == False:
time.sleep(0.1)
print ("Shoot")
showImage(blackImage)
time.sleep(1)
showImage(pattern)
The result would be that when the button is pressed, the black image will be shown on the screen and then the image of the pattern, but only the black image will appear and when the second image is not replaced by the image of the pattern, it will not be closed at the same time. press Esc, I have to press Alt + F4.