Coverage for ase/gui/icons.py: 90.91%

33 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-11-27 15:21 +0000

1import tkinter as tk 

2from pathlib import Path 

3 

4 

5class Icons: 

6 def __init__(self): 

7 icon_dir = Path(__file__).parent / 'icons' 

8 self.files = {} 

9 self.files['play'] = icon_dir / 'CarbonPlay.png' 

10 self.files['pause'] = icon_dir / 'CarbonPause.png' 

11 self.files['back'] = icon_dir / 'CarbonArrowLeft.png' 

12 self.files['forward'] = icon_dir / 'CarbonArrowRight.png' 

13 self.files['first'] = icon_dir / 'ASESkipBack.png' 

14 self.files['last'] = icon_dir / 'ASESkipForward.png' 

15 self.files['movie'] = icon_dir / 'CarbonDataPlayer.png' 

16 self.files['graph'] = icon_dir / 'CarbonChartLine.png' 

17 

18 self.images = {} 

19 

20 self.scale = 3.0 

21 

22 def create_photoimages(self): 

23 """Creates PhotoImage objects that Tkinter can use. This must be done 

24 after the main window is created.""" 

25 # Pillow is super convenient here, but it then needs to be added as 

26 # a dependency to ASE 

27 try: 

28 from PIL import Image, ImageTk 

29 except ModuleNotFoundError: 

30 ImageTk = None 

31 

32 for key in self.files: 

33 if ImageTk is None: 

34 self.images[key] = tk.PhotoImage( 

35 file=self.files[key] 

36 ).subsample(2) 

37 else: 

38 img = Image.open(fp=self.files[key]) 

39 dim = round(self.scale * 20) 

40 self.images[key] = ImageTk.PhotoImage(img.resize([dim, dim])) 

41 

42 def set_scaling(self, scale): 

43 self.scale = scale 

44 

45 def __getitem__(self, key): 

46 if not self.images: 

47 self.create_photoimages() 

48 return self.images[key]