2017-01-15 3 views
0

나는 Rasbperry Pi 3을 설정하고 Python 3을 사용했다. 나는 이미지를 찍을 수는 있지만 화면에서 그들을 새로 고쳐야한다. Tkinker에 아주 새로운 기능. 몇 가지 코드가 있지만 좋은 크기의 이미지를 캔버스에 배치하고 내 버튼 중 하나를 누를 때이를 새로 고치는 방법을 알고 싶습니다.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()