2017-01-15 125 views
0

我建立了一個Rasbperry Pi 3並使用Python 3.我可以拍攝圖像,但需要在屏幕上刷新它們。非常新的tkinker。我有一些代碼,但想知道如何在畫布上放置一個尺寸不錯的圖像,並在按下其中一個按鈕時刷新它。python 3 tkinter圖像

#!/usr/bin/python 

import tkinter 
import RPi.GPIO as GPIO 
import picamera 
import time 
from PIL import Image, ImageTk 




def startup(): 
    def callback(a): 

     if a==4: 
      thiscolour="yellow" 
      camera.capture('/home/pi/Desktop/image.jpg') 
     if a==2: 
      thiscolour="red" 
     lbl.configure(text=thiscolour+" has been pressed") 



     path = "/home/pi/Desktop/image.jpg" 
     img = Image.open(path) 
     new_width = 400 
     new_height = 600 
     #img = img.resize((new_width, new_height), Image.ANTIALIAS) 
     img.save('thisimage.jpg') 
     path="thisimage.jpg" 
     path="thisimage.jpg" 
     image = Image.open(path) 
     photo = ImageTk.PhotoImage(image) 
     img = ImageTk.PhotoImage(Image.open(path)) 
     panel.configure(image=photo) 
     panel.pack() 




    camera = picamera.PiCamera() 
    path = "/home/pi/Desktop/image.jpg" 
    img = Image.open(path) 
    new_width = 400 
    new_height = 600 
    img = img.resize((new_width, new_height), Image.ANTIALIAS) 
    img.save('thisimage.jpg') 
    path="thisimage.jpg" 
    GPIO.setmode(GPIO.BCM) 
    GPIO.setwarnings(False) 
    buttonyellow = 4 
    buttonred = 2 
    t=0 
    GPIO.setup(buttonyellow, GPIO.IN, GPIO.PUD_UP) 
    GPIO.setup(buttonred, GPIO.IN, GPIO.PUD_UP) 
    window=tkinter.Tk() 
    window.title("Photobooth") 
    window.geometry("1000x800") 
    lbl=tkinter.Label(window,text="Instructions") 
    ent=tkinter.Entry(window) 
    btn=tkinter.Button(window,text="Press here", bg='red') 
    btn=tkinter.Button(window,text="Click me",bg='red',command=callback) 
    btn.pack() 
    lbl.pack() 
    ent.pack() 
    btn.pack() 
    GPIO.add_event_detect(buttonyellow, GPIO.FALLING, callback=callback, bouncetime=100) 
    GPIO.add_event_detect(buttonred, GPIO.FALLING, callback=callback, bouncetime=100) 
    path="thisimage.jpg" 
    image = Image.open(path) 
    photo = ImageTk.PhotoImage(image) 
    img = ImageTk.PhotoImage(Image.open(path)) 
    panel = tkinter.Label(window, image = img) 
    panel.pack(side = "bottom", fill = "both", expand = "yes") 
    window.mainloop()  


startup() 
+0

'按鈕(...,命令=執行)'和'canvas.create_image(...)' – furas

+0

更好找一些教程-http://effbot.org/tkinterbook/ – furas

+0

感謝。我發現很多教程,但我似乎只是在這方面卡住了。 – stixson99

回答

1

圖像顯示在標籤中。更新按鈕觸發一個命令,首先拍攝新照片(或任何你想要的,我沒有寫這部分代碼),然後加載新的圖像,最後使用configure(image=...)更新標籤內的圖像。

import tkinter as tk 
from PIL import Image, ImageTk 

class App(tk.Tk): 
    def __init__(self): 
     tk.Tk.__init__(self) # create window 
     # load initial image 
     self.img = ImageTk.PhotoImage(Image.open("path/to/image")) 
     # display it in a label 
     self.label = tk.Label(self, image=self.img) 
     self.label.pack(fill='both', expand=True) 

     tk.Button(self, text="Update", command=self.update_image).pack() 

     self.mainloop() 

    def update_image(self): 
     # code to capture new image here 
     # ... 
     # load new image 
     self.img = ImageTk.PhotoImage(Image.open("path/to/image")) 
     # update label image 
     self.label.configure(image=self.img) 

if __name__ == '__main__': 
    App()