简体   繁体   English

我需要帮助使用 tkinter 显示全屏图像

[英]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.现在我正在使用下面的代码(我从这里的另一个问题 xd 中获取)来更改 tkinter 正在显示的图像。 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.问题在于它只显示图片 cat.jpg 一秒钟,然后显示黑屏,没有任何反应。

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() :首先,您应该在updateRoot()之外执行以下操作:

  • make root window fullscreen (you can simply use root.attributes('-fullscreen', 1) )使 root window 全屏(您可以简单地使用root.attributes('-fullscreen', 1)
  • bind the <Escape> key绑定<Escape>
  • create the canvas and create_image() (you can use Label to do the same thing)创建 canvas 和create_image() (您可以使用Label做同样的事情)

Then just update the image inside updateRoot() .然后只需更新updateRoot()中的图像。

Also you should use after() instead of time.sleep() .你也应该使用after()而不是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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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