|
4 | 4 | import tkinter as tk
|
5 | 5 | from PIL import Image, ImageTk
|
6 | 6 |
|
| 7 | + |
| 8 | + |
7 | 9 | class AnimatedLabel(tk.Label):
|
8 | 10 |
|
9 |
| - def __init__(self, *args, images=None, delay=100, state='Idle', **kwargs): |
| 11 | + def __init__(self, *args, images=None, delay=100, **kwargs): |
10 | 12 | super().__init__(*args, **kwargs)
|
11 | 13 |
|
12 | 14 | self.images = images
|
13 | 15 | self.delay = delay
|
14 |
| - self.state = state |
| 16 | + |
15 | 17 | self.current_frame = 0
|
| 18 | + self.running = False |
| 19 | + |
| 20 | + self.job_id = None |
| 21 | + |
| 22 | + def start(self): |
| 23 | + if not self.running: |
| 24 | + self.running = True |
| 25 | + self.next_frame() |
| 26 | + else: |
| 27 | + print("already running") |
16 | 28 |
|
17 |
| - def start(self): |
18 |
| - self.next_frame() |
19 |
| - |
| 29 | + def stop(self): |
| 30 | + if self.running: |
| 31 | + self.running = False |
| 32 | + if self.job_id: |
| 33 | + self.after_cancel(self.job_id) |
| 34 | + else: |
| 35 | + print("not running") |
| 36 | + |
20 | 37 | def next_frame(self):
|
21 |
| - if self.state == 'Idle': |
| 38 | + |
| 39 | + if self.running: |
22 | 40 | # change image
|
23 | 41 | self.frame = self.images[self.current_frame]
|
24 | 42 | self.configure(image=self.frame)
|
25 | 43 |
|
26 | 44 | # get next frame (or first frame)
|
27 | 45 | self.current_frame = (self.current_frame+1) % len(self.images)
|
28 | 46 |
|
29 |
| - # run again after 'delay' milliseconds |
30 |
| - self.after(self.delay, self.next_frame) |
| 47 | + # run again after 'delay' milliseconds |
| 48 | + self.job_id = self.after(self.delay, self.next_frame) |
31 | 49 |
|
32 |
| -class Application(): |
33 | 50 |
|
34 |
| - def __init__(self): |
35 |
| - |
36 |
| - # WINDOW SETUP |
37 |
| - self.root = tk.Tk() |
38 |
| - self.root.geometry('512x512') |
39 |
| - self.root.protocol('WM_DELETE_WINDOW', self.annihilation) |
| 51 | +class Application(tk.Tk): |
| 52 | + |
| 53 | + def __init__(self, width=500, height=500): |
| 54 | + super().__init__() |
| 55 | + |
| 56 | + #self.geometry('{}x{}'.format(width, height)) |
| 57 | + self.protocol('WM_DELETE_WINDOW', self.on_exit) |
40 | 58 |
|
| 59 | + # images for both animations |
41 | 60 | self.load_images()
|
42 | 61 |
|
43 |
| - # CALL THE ANIMATION FUNCTION |
44 |
| - self.animation1 = AnimatedLabel(self.root, images=self.images, delay=500) |
45 |
| - self.animation1.place(relx=.5, rely=.5, anchor="c") |
| 62 | + # create two aniamted labels with different speed/delay |
| 63 | + |
| 64 | + self.animation1 = AnimatedLabel(self, images=self.images, delay=500) |
| 65 | + self.animation1.pack(side='left') |
46 | 66 |
|
| 67 | + self.animation2 = AnimatedLabel(self, images=self.images, delay=250) |
| 68 | + self.animation2.pack(side='left') |
| 69 | + |
| 70 | + self.animation3 = AnimatedLabel(self, images=self.images, delay=125) |
| 71 | + self.animation3.pack(side='left') |
47 | 72 |
|
48 |
| - self.animation2 = AnimatedLabel(self.root, images=self.images, delay=125) |
49 |
| - self.animation2.place(relx=.1, rely=.1, anchor="c") |
| 73 | + # start both at the same moment |
50 | 74 |
|
51 | 75 | self.animation1.start()
|
52 | 76 | self.animation2.start()
|
| 77 | + self.animation3.start() |
53 | 78 |
|
54 |
| - self.root.mainloop() |
55 |
| - |
| 79 | + def run(self): |
| 80 | + self.mainloop() |
56 | 81 |
|
57 |
| - # LIST OF IMAGES |
58 |
| - def load_images(self): |
| 82 | + def load_images(self, size=None): |
59 | 83 | self.filenames = [
|
60 |
| - 'Obrazy/21586784_1642005829205281_8345912444787042013_o.jpg', |
61 |
| - 'Obrazy/37884728_1975437959135477_1313839270464585728_n.jpg' |
62 |
| - #'Image1.tif', |
63 |
| - #'Image2.tif', |
| 84 | + 'ball1.png', |
| 85 | + 'ball2.png', |
| 86 | + 'ball3.png', |
64 | 87 | ]
|
65 | 88 | self.images = []
|
66 | 89 | for filename in self.filenames:
|
67 |
| - img = ImageTk.PhotoImage(Image.open(filename).resize((512, 512))) |
| 90 | + img = Image.open(filename) |
| 91 | + if size: |
| 92 | + img = img.resize((size, size)) |
| 93 | + img = ImageTk.PhotoImage(img) |
68 | 94 | self.images.append(img)
|
69 | 95 |
|
70 |
| - def annihilation(self): |
71 |
| - #self.root.eval('::ttk::CancelRepeat') |
72 |
| - self.state = 'Quitting' |
73 |
| - self.root.destroy() |
| 96 | + def on_exit(self): |
| 97 | + #self.eval('::ttk::CancelRepeat') |
| 98 | + self.destroy() |
74 | 99 |
|
75 |
| -Application() |
| 100 | +if __name__ == '__main__': |
| 101 | + app = Application() |
| 102 | + app.run() |
0 commit comments