2017-01-15 6 views
0

私はRasbperry Pi 3をセットアップし、Python 3を使用しています。私はイメージを取ることができますが、画面上でそれらをリフレッシュする必要があります。非常に新しいtkinker。私はいくつかのコードを持っていますが、キャンバスにすばらしいサイズのイメージを配置し、ボタンの1つが押されたときにそれをリフレッシュする方法を知りたいと思います。python 3 tkinter images

#!/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

'ボタン(...、コマンド= FUNCTION_NAME)'と '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() 
関連する問題