Skip to content

Commit e2f6a42

Browse files
committed
new examples
1 parent b97456b commit e2f6a42

File tree

10 files changed

+73
-34
lines changed

10 files changed

+73
-34
lines changed

__images__/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
Images used in some examples.
3+
4+
I put them here so I don't have to search new images for new examples.
5+
6+
![#1](ball1.gif?raw=true)
7+
![#2](ball2.gif?raw=true)
8+
![#3](ball3.gif?raw=true)

__images__/ball1.gif

2.04 KB
Loading

__images__/ball2.gif

2.01 KB
Loading

__images__/ball3.gif

2.01 KB
Loading

tkinter/animated-label/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# AnimatedLabel
3+
4+
![#1](images/image.png?raw=true)

tkinter/animated-label/ball1.png

4.43 KB
Loading

tkinter/animated-label/ball2.png

4.86 KB
Loading

tkinter/animated-label/ball3.png

4.7 KB
Loading
14.5 KB
Loading

tkinter/animated-label/main.py

Lines changed: 61 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -4,72 +4,99 @@
44
import tkinter as tk
55
from PIL import Image, ImageTk
66

7+
8+
79
class AnimatedLabel(tk.Label):
810

9-
def __init__(self, *args, images=None, delay=100, state='Idle', **kwargs):
11+
def __init__(self, *args, images=None, delay=100, **kwargs):
1012
super().__init__(*args, **kwargs)
1113

1214
self.images = images
1315
self.delay = delay
14-
self.state = state
16+
1517
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")
1628

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+
2037
def next_frame(self):
21-
if self.state == 'Idle':
38+
39+
if self.running:
2240
# change image
2341
self.frame = self.images[self.current_frame]
2442
self.configure(image=self.frame)
2543

2644
# get next frame (or first frame)
2745
self.current_frame = (self.current_frame+1) % len(self.images)
2846

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)
3149

32-
class Application():
3350

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)
4058

59+
# images for both animations
4160
self.load_images()
4261

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')
4666

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')
4772

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
5074

5175
self.animation1.start()
5276
self.animation2.start()
77+
self.animation3.start()
5378

54-
self.root.mainloop()
55-
79+
def run(self):
80+
self.mainloop()
5681

57-
# LIST OF IMAGES
58-
def load_images(self):
82+
def load_images(self, size=None):
5983
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',
6487
]
6588
self.images = []
6689
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)
6894
self.images.append(img)
6995

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()
7499

75-
Application()
100+
if __name__ == '__main__':
101+
app = Application()
102+
app.run()

0 commit comments

Comments
 (0)