Coverage for /builds/kinetik161/ase/ase/gui/widgets.py: 77.42%

62 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-12-10 11:04 +0000

1import ase.data 

2import ase.gui.ui as ui 

3from ase import Atoms 

4from ase.gui.i18n import _ 

5 

6 

7class Element(list): 

8 def __init__(self, symbol='', callback=None): 

9 list.__init__(self, 

10 [_('Element:'), 

11 ui.Entry(symbol, 3, self.enter), 

12 ui.Button(_('Help'), self.show_help), 

13 ui.Label('', 'red')]) 

14 self.callback = callback 

15 

16 @property 

17 def z_entry(self): 

18 return self[1] 

19 

20 def grab_focus(self): 

21 self.z_entry.entry.focus_set() 

22 

23 def show_help(self): 

24 msg = _('Enter a chemical symbol or the atomic number.') 

25 # Title of a popup window 

26 ui.showinfo(_('Info'), msg) 

27 

28 @property 

29 def Z(self): 

30 atoms = self.get_atoms() 

31 if atoms is None: 

32 return None 

33 assert len(atoms) == 1 

34 return atoms.numbers[0] 

35 

36 @property 

37 def symbol(self): 

38 Z = self.Z 

39 return None if Z is None else ase.data.chemical_symbols[Z] 

40 

41 # Used by tests... 

42 @symbol.setter 

43 def symbol(self, value): 

44 self.z_entry.value = value 

45 

46 def get_atoms(self): 

47 val = self._get() 

48 if val is not None: 

49 self[2].text = '' 

50 return val 

51 

52 def _get(self): 

53 txt = self.z_entry.value 

54 

55 if not txt: 

56 self.error(_('No element specified!')) 

57 return None 

58 

59 if txt.isdigit(): 

60 txt = int(txt) 

61 try: 

62 txt = ase.data.chemical_symbols[txt] 

63 except KeyError: 

64 self.error() 

65 return None 

66 

67 if txt in ase.data.atomic_numbers: 

68 return Atoms(txt) 

69 

70 self.error() 

71 

72 def enter(self): 

73 self.callback(self) 

74 

75 def error(self, text=_('ERROR: Invalid element!')): 

76 self[2].text = text 

77 

78 

79def pybutton(title, callback): 

80 """A button for displaying Python code. 

81 

82 When pressed, it opens a window displaying some Python code, or an error 

83 message if no Python code is ready. 

84 """ 

85 return ui.Button('Python', pywindow, title, callback) 

86 

87 

88def pywindow(title, callback): 

89 code = callback() 

90 if code is None: 

91 ui.error( 

92 _('No Python code'), 

93 _('You have not (yet) specified a consistent set of parameters.')) 

94 else: 

95 win = ui.Window(title, wmtype='utility') 

96 win.add(ui.Text(code))