Ewindow.py 7.83 KB
Newer Older
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
# -*- coding: iso-8859-15 -*-

import Tkinter as tk
from PTE import *
from numpy import ones

Edict = dict()
Ebuttons = list()
i=0

points = []
spline = 0
tag1 = "theline"
Econtrol = 0

##############################################################################
# PTE window

#PTE = tk.Tk()
#PTE.title('Periodic Table of Elements')

##############################################################################
# Element callback

def elem_callback(x, edict , ebutton, Labelmass, LabelZ, Labelname, Labelsymbol, Entrymass, EntryZ, Entryname, Entrysymbol):
    PTable(edict[x] , ebutton[x], Labelmass, LabelZ, Labelname, Labelsymbol, Entrymass, EntryZ, Entryname, Entrysymbol)

##############################################################################
# Element selection

def elem_select(i, button, Labelmass, LabelZ, Labelname, Labelsymbol, Entrymass, EntryZ, Entryname, Entrysymbol):

    global Econtrol, Edict
    button[Econtrol]['relief']='raised'
    Econtrol = i
    button[i]['relief']='sunken'

    Labelmass.configure(text = 'Mass - %f' %Edict[i]['mass'])
    LabelZ.configure(text = 'Z - %f' %Edict[i]['Z'])
    Labelname.configure(text = '%s' %Edict[i]['name'])
    Labelsymbol.configure(text = 'Symbol - %s' %Edict[i]['symbol'])

    Entrymass.delete (0, len(Entrymass.get()))
    Entryname.delete (0, len(Entryname.get()))
    EntryZ.delete (0, len(EntryZ.get()))
    Entrysymbol.delete (0, len(Entrysymbol.get()))

    Entrymass.insert(0, Edict[int(Econtrol)]['mass'])
    Entryname.insert(0, Edict[int(Econtrol)]['name'])
    EntryZ.insert(0, Edict[int(Econtrol)]['Z'])
    Entrysymbol.insert(0, Edict[int(Econtrol)]['symbol'])

    #if Edict[i]['name'] == 'New':
    #    butPTE.invoke()

##############################################################################
# Init

def ewin_build(window, xmax, xstep):


##############################################################################
# Element addition

    def create():
        global i, Edict, Ebuttons
        Edict[i] = dict(name = "New", symbol = "Hf", mass = 178.00, Z = 72, dist = ones(int(xmax/xstep)), control = 0, LineShape = 200.)
        Ebuttons.insert(i, tk.Button(Eframe, text=i, width=1, height=1, command = lambda i=i : elem_select(i,Ebuttons,Labelmass,LabelZ,Labelname,Labelsymbol,Entrymass,EntryZ,Entryname,Entrysymbol)))
        Ebuttons[i].pack(side='left')
        i=i+1

##############################################################################
# Element deletion

    def remove():
        global i, Edict, Ebuttons, Econtrol
        if i > 1:
            i=i-1
            del Edict[i]
            Ebuttons[i].destroy()
            del Ebuttons[i]
            if Econtrol == i:
                Econtrol = Econtrol -1
                Ebuttons[Econtrol]['relief']='sunken'

##############################################################################
# Element Frames

    Label1 = tk.LabelFrame(window, text = 'Elements', relief='raised', bd=2)
    Label1.pack() 

    Eframe = tk.Canvas(Label1)
    Eframe.pack(fill='both',expand=0)
    
##############################################################################
# Init
    create()

##############################################################################
# Properties

    global Econtrol, Edict
    def update(i):
        Edict[i]['mass'] = float(Entrymass.get())
        Labelmass.configure(text = 'Mass - %f' %Edict[i]['mass'])
        Edict[i]['Z'] = float(EntryZ.get())
        LabelZ.configure(text = 'Z - %f' %Edict[i]['Z'])
        Edict[i]['name'] = str(Entryname.get())
        Labelname.configure(text = '%s' %Edict[i]['name'])
        Edict[i]['symbol'] = str(Entrysymbol.get())
        Labelsymbol.configure(text = 'Symbol - %s' %Edict[i]['symbol'])
        #Edict[i]['LineShape'] = float(EntryLS.get())
        #LabelLS.configure(text = 'Line Shape α - %f' %Edict[i]['LineShape'])
        print Edict[i]

    Labelprop = tk.LabelFrame(window, text = 'Properties', relief='raised', bd=2)
    #Labelprop.pack()
    Labelprop.place(anchor='w', y=100, x=5)   

    LpFrame1 = tk.Frame(Labelprop)
    LpFrame1.pack(side='left')
    LpFrame2 = tk.Frame(Labelprop)
    LpFrame2.pack(side='top')
    LpFrame3 = tk.Frame(Labelprop)
    LpFrame3.pack(side='right')

    EPframel = tk.Frame(LpFrame1)
    EPframel.pack(side='left')
    EPframee = tk.Frame(LpFrame1)
    EPframee.pack(side='right')

    EPframel2 = tk.Frame(LpFrame2)
    EPframel2.pack(side='left')
    EPframee2 = tk.Frame(LpFrame2)
    EPframee2.pack(side='right')

    Labelname = tk.Label(EPframel, width=20, pady=2, text='%s' %Edict[int(Econtrol)]['name'])
    Labelmass = tk.Label(EPframel, width=20, pady=2, text='Mass - %f' %Edict[int(Econtrol)]['mass'])
    LabelZ = tk.Label(EPframel, width=20, pady=2, text='Z - %f' %Edict[int(Econtrol)]['Z'])
    Labelsymbol = tk.Label(EPframel, width=20, pady=2, text= 'Symbol - %s' %Edict[int(Econtrol)]['symbol'])
    #LabelLS = tk.Label(EPframel, width=30, pady=2, text='Line Shape α - %f' %Edict[int(Econtrol)]['LineShape'])
    Labelname.pack()
    Labelmass.pack()
    LabelZ.pack()
    Labelsymbol.pack()
    #LabelLS.pack()

    Entryname = tk.Entry(EPframee, width=15)
    Entrymass = tk.Entry(EPframee, width=15)
    EntryZ = tk.Entry(EPframee, width=15)
    Entrysymbol = tk.Entry(EPframee, width=15)
    #EntryLS = tk.Entry(EPframee, width=22)
    Entryname.pack()
    Entrymass.pack()
    EntryZ.pack()
    Entrysymbol.pack()
    #EntryLS.pack()

    BUpdate = tk.Button(EPframee2, text ='Update properties', command=lambda i=i :update(Econtrol), width=15)
    BUpdate.pack()#side='right')
    BUpdateD = tk.Button(EPframee2, text ='Update Distribution', command=lambda i=i :update(Econtrol), width=15)
    BUpdateD.pack()#side='left')

    but = tk.Button(EPframel2, command=create, text='Add element', bd=1, height=1,width=15)
    but.pack()#side='left') 

    butrmv = tk.Button(EPframel2, command=remove, text='Remove element', bd=1, height=1,width=15)
    butrmv.pack()#side='right')

    butPTE = tk.Button(EPframel2, text='Show PTE', bd=1, width=15, command=lambda i=i :elem_callback(Econtrol,Edict,Ebuttons,Labelmass,LabelZ,Labelname,Labelsymbol,Entrymass,EntryZ,Entryname,Entrysymbol))
    butPTE.pack()#side='left')

##############################################################################
# Init
    Entrymass.insert(0, Edict[int(Econtrol)]['mass'])
    Entryname.insert(0, Edict[int(Econtrol)]['name'])
    EntryZ.insert(0, Edict[int(Econtrol)]['Z'])
    Entrysymbol.insert(0, Edict[int(Econtrol)]['symbol'])
    #EntryLS.insert(0, Edict[int(Econtrol)]['LineShape'])

##############################################################################
# Canvas callback
    def point(event):
        Distcanvas.create_oval(event.x, event.y, event.x+1, event.y+1, fill="black")
        points.append(event.x)
        points.append(event.y)
        return points

    def canxy(event):
        print event.x, event.y

    def graph(event):
        global theline
        Distcanvas.create_line(points, tags="theline")

    def toggle(event):
        global spline
        if spline == 0:
            Distcanvas.itemconfigure(tag1, smooth=1)
            spline = 1
        elif spline == 1:
            Distcanvas.itemconfigure(tag1, smooth=0)
            spline = 0
        print event.x, event.y
        return spline

##############################################################################
# Drawing Canvas

    Label2 = tk.LabelFrame(window, text = 'Distribution', bd=1)
    #Label2.pack()   
    Label2.place(anchor='w', y=320, x=5)  

    Distcanvas = tk.Canvas(Label2, bg="white", width=800, height= 300)
    #Distcanvas.configure(cursor="crosshair")
    Distcanvas.grid_columnconfigure(999, weight = 1)
    Distcanvas.grid_rowconfigure(999, weight = 1)
    Distcanvas.pack()
    Distcanvas.bind("<Button-1>", point)
    Distcanvas.bind("<Button-3>", graph)
    Distcanvas.bind("<Button-2>", toggle)

##############################################################################