Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1from ase.gui.i18n import _ 

2 

3import ase.data 

4import ase.gui.ui as ui 

5 

6from ase import Atoms 

7 

8 

9class Element(list): 

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

11 list.__init__(self, 

12 [_('Element:'), 

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

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

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

16 self.callback = callback 

17 

18 @property 

19 def z_entry(self): 

20 return self[1] 

21 

22 def grab_focus(self): 

23 self.z_entry.entry.focus_set() 

24 

25 def show_help(self): 

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

27 # Title of a popup window 

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

29 

30 @property 

31 def Z(self): 

32 atoms = self.get_atoms() 

33 if atoms is None: 

34 return None 

35 assert len(atoms) == 1 

36 return atoms.numbers[0] 

37 

38 @property 

39 def symbol(self): 

40 Z = self.Z 

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

42 

43 # Used by tests... 

44 @symbol.setter 

45 def symbol(self, value): 

46 self.z_entry.value = value 

47 

48 def get_atoms(self): 

49 val = self._get() 

50 if val is not None: 

51 self[2].text = '' 

52 return val 

53 

54 def _get(self): 

55 txt = self.z_entry.value 

56 

57 if not txt: 

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

59 return None 

60 

61 if txt.isdigit(): 

62 txt = int(txt) 

63 try: 

64 txt = ase.data.chemical_symbols[txt] 

65 except KeyError: 

66 self.error() 

67 return None 

68 

69 if txt in ase.data.atomic_numbers: 

70 return Atoms(txt) 

71 

72 self.error() 

73 

74 def enter(self): 

75 self.callback(self) 

76 

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

78 self[2].text = text 

79 

80 

81def pybutton(title, callback): 

82 """A button for displaying Python code. 

83 

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

85 message if no Python code is ready. 

86 """ 

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

88 

89 

90def pywindow(title, callback): 

91 code = callback() 

92 if code is None: 

93 ui.error( 

94 _('No Python code'), 

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

96 else: 

97 win = ui.Window(title) 

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