jeudi 23 juin 2016

How can i reload a picture with Tkinter after GPIO button press?


I have the following script based on a solution of another Stackoverflow question. I have a tkinter GUI with a picture that should refresh when there is taken a new one.

class App(threading.Thread):
    def __init__(self):
        threading.Thread.__init__(self)
        self.start()

    def callback(self):
        self.root.quit()

    def run(self):
        self.root = Tk()
        self.root.geometry("{0}x{1}+0+0".format(800,800))

        files = sorted(os.listdir(os.getcwd()),key=os.path.getmtime)

        Label(self.root,image = ImageTk.PhotoImage(Image.open(files[-1]).resize((300, 200)))).grid(row = 1, column = 0)

        Label(self.root, text=files[-1]).grid(row=0, column=0)
        self.root.mainloop()

    def update(self):

        files = sorted(os.listdir(os.getcwd()),key=os.path.getmtime)

        img1 = self.ImageTk.PhotoImage(self.Image.open(files[-1]).resize((300, 200)))
        Label(self.root,image = img1).grid(row = 1, column = 0)

        Label(self.root, text=files[-1]).grid(row=0, column=0)

        self.root.update()

app = App()        
app.update()

I get this error:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 176, in paste
    tk.call("PyImagingPhoto", self.__photo, block.id)
_tkinter.TclError: invalid command name "PyImagingPhoto"

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "./mainloop.py", line 98, in <module>
    app.update()
  File "./mainloop.py", line 79, in update
    img1 = ImageTk.PhotoImage(Image.open(files[-1]).resize((300, 200)))
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 115, in __init__
    self.paste(image)
  File "/usr/lib/python3/dist-packages/PIL/ImageTk.py", line 182, in paste
    _imagingtk.tkinit(tk.interpaddr(), 1)
OverflowError: Python int too large to convert to C ssize_t

What am i doing wrong? Creating a label with with the file name does work. The filename is a valid file. The script does work when it is not inside the thread.Threading part. I would like to update the screen with the newest picture when a Raspberry GPIO button is pressed (script will take a new photo with a camara). So the screen should load with the lastest taken picture.


Aucun commentaire:

Enregistrer un commentaire