root / ase / gui / pybutton.py @ 4
Historique | Voir | Annoter | Télécharger (2,3 ko)
| 1 |
|
|---|---|
| 2 |
"""pybutton.py - a button for displaying Python code.
|
| 3 |
|
| 4 |
This module defines two classes, together they implement a button that
|
| 5 |
a module can use to display Python.
|
| 6 |
|
| 7 |
PyButton
|
| 8 |
--------
|
| 9 |
|
| 10 |
PyButton is a gkt.Button with the label 'Python'. When pressed, it
|
| 11 |
opens a PyWindow displaying some Python code, or an error message if
|
| 12 |
no Python code is ready.
|
| 13 |
|
| 14 |
The script is stored in the attribute .python, it is the
|
| 15 |
responsability of the owning object to keep this attribute up to date:
|
| 16 |
when pressing the Apply button would result in a sensible
|
| 17 |
configuration being created, the python attribute must be set to a
|
| 18 |
string creating this code. When pressing Apply would cause an error,
|
| 19 |
the python attribute must be set to None.
|
| 20 |
|
| 21 |
PyWindow
|
| 22 |
--------
|
| 23 |
|
| 24 |
Displays the Python code. This object is created by the PyButton
|
| 25 |
object when needed.
|
| 26 |
"""
|
| 27 |
|
| 28 |
import gtk |
| 29 |
import time |
| 30 |
from ase.gui.widgets import oops, pack |
| 31 |
|
| 32 |
|
| 33 |
class PyButton(gtk.Button): |
| 34 |
"A button for displaying Python code."
|
| 35 |
def __init__(self, title): |
| 36 |
gtk.Button.__init__(self, "Python") |
| 37 |
self.title = title
|
| 38 |
self.python = None |
| 39 |
self.connect_after('clicked', self.run) |
| 40 |
|
| 41 |
def run(self, *args): |
| 42 |
"The method called when the button is click."
|
| 43 |
if self.python: |
| 44 |
now = time.ctime() |
| 45 |
win = PyWindow(self.title, now, self.python) |
| 46 |
else:
|
| 47 |
oops("No Python code",
|
| 48 |
"You have not (yet) specified a consistent set of parameters.")
|
| 49 |
|
| 50 |
fr1_template = """
|
| 51 |
Title: %s
|
| 52 |
Time: %s
|
| 53 |
"""
|
| 54 |
|
| 55 |
class PyWindow(gtk.Window): |
| 56 |
"A window displaying Python code."
|
| 57 |
def __init__(self, title, time, code): |
| 58 |
gtk.Window.__init__(self)
|
| 59 |
self.set_title("ag: Python code") |
| 60 |
vbox = gtk.VBox() |
| 61 |
lbl = gtk.Label(fr1_template % (title, time)) |
| 62 |
lbl.set_alignment(0.0, 0.5) |
| 63 |
fr = gtk.Frame("Information:")
|
| 64 |
fr.add(lbl) |
| 65 |
pack(vbox, fr) |
| 66 |
txtbuf = gtk.TextBuffer() |
| 67 |
txtbuf.set_text(code) |
| 68 |
txtview = gtk.TextView(txtbuf) |
| 69 |
txtview.set_editable(False)
|
| 70 |
fr = gtk.Frame("Python code:")
|
| 71 |
fr.add(txtview) |
| 72 |
fr.set_label_align(0.0, 0.5) |
| 73 |
pack(vbox, fr) |
| 74 |
but = gtk.Button(stock=gtk.STOCK_OK) |
| 75 |
but.connect('clicked', lambda x: self.destroy()) |
| 76 |
pack(vbox, [but], end=True, bottom=True) |
| 77 |
self.add(vbox)
|
| 78 |
self.show_all()
|
| 79 |
|