1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
import ase.data
import ase.gui.ui as ui
from ase import Atoms
from ase.gui.i18n import _
class Element(list):
def __init__(self, symbol='', callback=None):
list.__init__(self,
[_('Element:'),
ui.Entry(symbol, 3, self.enter),
ui.Button(_('Help'), self.show_help),
ui.Label('', 'red')])
self.callback = callback
@property
def z_entry(self):
return self[1]
def grab_focus(self):
self.z_entry.entry.focus_set()
def show_help(self):
msg = _('Enter a chemical symbol or the atomic number.')
# Title of a popup window
ui.showinfo(_('Info'), msg)
@property
def Z(self):
atoms = self.get_atoms()
if atoms is None:
return None
assert len(atoms) == 1
return atoms.numbers[0]
@property
def symbol(self):
Z = self.Z
return None if Z is None else ase.data.chemical_symbols[Z]
# Used by tests...
@symbol.setter
def symbol(self, value):
self.z_entry.value = value
def get_atoms(self):
val = self._get()
if val is not None:
self[2].text = ''
return val
def _get(self):
txt = self.z_entry.value
if not txt:
self.error(_('No element specified!'))
return None
if txt.isdigit():
txt = int(txt)
try:
txt = ase.data.chemical_symbols[txt]
except KeyError:
self.error()
return None
if txt in ase.data.atomic_numbers:
return Atoms(txt)
self.error()
def enter(self):
self.callback(self)
def error(self, text=_('ERROR: Invalid element!')):
self[2].text = text
def pybutton(title, callback):
"""A button for displaying Python code.
When pressed, it opens a window displaying some Python code, or an error
message if no Python code is ready.
"""
return ui.Button('Python', pywindow, title, callback)
def pywindow(title, callback):
code = callback()
if code is None:
ui.error(
_('No Python code'),
_('You have not (yet) specified a consistent set of parameters.'))
else:
win = ui.Window(title, wmtype='utility')
win.add(ui.Text(code))
|