-2

In Tkinter, I need to have a GIF image move across a window every 2 seconds, and when it's clicked on to have a splash screen show. I have absolutely no idea how to import an image and have it move / respond to a click, and I can't find a straight answer elsewhere.

Thanks

Thanos
  • 1
  • You can be more precise with your question and also write a bit about what your tried and what you got stuck on. Due to your general question you got a few down-votes. If my answer helps you, you can upvote it and and accept it as the right answer. If you it doesn't you can leave a comment and I might check back (or someone else might see it). – Eran Sep 14 '18 at 07:58

1 Answers1

1

Here's a quick example, some notes:

  1. tkinter doesn't animate gifs, you can do it manually if you want (link)
  2. This is Python3 code, in Python2 some things are different (import names, calling super, etc).
  3. The label has a background even if the image is transparent, there are workarounds (link)
  4. In this example we take an image (named s.gif and placed next to the code file) and place it at (0,0) then every 2seconds it moves by 10 in both x and y (mod size of frame). when it gets clicked we show a splash frame for a second and then remove it (since that's the point of "splash")

from PIL import Image, ImageTk
from tkinter import Tk, BOTH, Toplevel
from tkinter.ttk import Frame, Label


class Example(Frame):
    def __init__(self, root):
        super().__init__()
        self._root = root

        # Init class variables
        self._size = 300
        self._image_label = None
        self.image_place = 0

        self.init_ui()

        # Acutally run the UI
        root.mainloop()

    def init_ui(self):
        # Init general layour
        self._root.geometry("{size}x{size}+{size}+{size}".format(size=self._size))
        self.master.title("Absolute positioning")
        self.pack(fill=BOTH, expand=1)  # show this frame

        # Setup image
        path_to_iamge = "s.gif"
        s_image = ImageTk.PhotoImage(Image.open(path_to_iamge).convert("RGBA")) # Convert to RGBA to get transparency
        # place image in a label, we are going to be moving this frame around
        self._image_label = Label(self, image=s_image)
        self._image_label.image = s_image
        # Make image clickable, running show_splash when clicked
        self._image_label.bind("<Button-1>", self._show_splash)

        self._place_image_label()

    def _place_image_label(self):
        # Show image label
        self._image_label.place(x=self.image_place, y=self.image_place)
        # increase image coordination for next time
        self.image_place = (self.image_place + 10) % self._size
        # reschedule to run again in 2 seconds
        self._root.after(2000, self._place_image_label)

    def _show_splash(self, event):
        splash = Toplevel(self)
        # update UI with splash
        self.update()
        # schedule to close splash after a 1 second
        self._root.after(1000, lambda: splash.destroy())


def main():
    Example(Tk())

if __name__ == '__main__':
    main()
Eran
  • 2,324
  • 3
  • 22
  • 27