Ewindow.py 14.3 KB
Newer Older
1
# -*- coding: iso-8859-15 -*-
2
3
import Tkinter as tk
from PTE import *
Matheus Müller's avatar
Matheus Müller committed
4
from pylab import *
5
from layers import *
Matheus Müller's avatar
Matheus Müller committed
6
7
from numpy import zeros
import os
8
import tkFileDialog
9
import shutil
10
import sys
11

12
13
##############################################################################
# Control variables and lists
Matheus Müller's avatar
Matheus Müller committed
14
Edict = dict()
15
Ebuttons = list()
16
Labentrys = list()
17
Frames = list()
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
18
i = 0
19
20
Econtrol = 0

Matheus Müller's avatar
Matheus Müller committed
21
##############################################################################
22
# Element callback
23
24
25
def elem_callback(x, edict , ebutton):
    global Labentrys
    PTable(edict[x] , ebutton[x], Labentrys)
26
27
28

##############################################################################
# Element selection
29
def elem_select(i, button):
30

31
    global Econtrol, Edict, Labentrys
32
    button[Econtrol]['relief']='raised'
33
    Econtrol = i
34
35
    button[i]['relief']='sunken'

36
37
38
39
40
41
    Labentrys[0].configure(text = '%s' %Edict[i]['name'])
    Labentrys[1].configure(text = 'Mass - %f' %Edict[i]['mass'])
    Labentrys[2].configure(text = 'Z - %f' %Edict[i]['Z'])
    Labentrys[3].configure(text = 'Symbol - %s' %Edict[i]['symbol'])
    Labentrys[4].configure(text = 'Line Shape α - %f' %Edict[i]['LineShape'])
    Labentrys[5].configure(text = 'Max Depht - %f' %Edict[i]['profundidademax'])
42

43
    for i in arange(6,12):
44
        Labentrys[i].delete (0, "end")
45

46
47
48
49
50
51
    Labentrys[6].insert(0, Edict[int(Econtrol)]['name'])
    Labentrys[7].insert(0, Edict[int(Econtrol)]['mass'])
    Labentrys[8].insert(0, int(Edict[int(Econtrol)]['Z']))
    Labentrys[9].insert(0, Edict[int(Econtrol)]['symbol'])
    Labentrys[10].insert(0, Edict[int(Econtrol)]['LineShape'])
    Labentrys[11].insert(0, Edict[int(Econtrol)]['profundidademax'])
52

Matheus Müller's avatar
Matheus Müller committed
53
##############################################################################
54
# Init
55
def ewin_build(window, OFlabentries, mainwindow, calcbutton, distvar, modevar, Labelframe):
56

57
58
##############################################################################
# Element addition
59
60
    def create():
        global i, Edict, Ebuttons
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
61
        Edict[i]=dict(name="New Element",symbol="Hf",mass=178.,Z=72,dist=zeros(int(10./float(OFlabentries[17].get()))),LineShape=200.,profundidademax=10.)
62
        Ebuttons.insert(i, tk.Button(Eframe, text=i, width=1, height=1, command = lambda i=i : elem_select(i,Ebuttons)) )
63
        Ebuttons[i].grid(column = ((len(Ebuttons)-1)%13), row = int(math.floor((len(Ebuttons)-1)/13)))
64
        i = i+1
65

66
67
##############################################################################
# Element deletion
68
    def remove():
69
        global i, Edict, Ebuttons, Econtrol
70
        if i > 1:
71
            i = i-1
72
73
74
75
            for a in range(Econtrol+1,i+1):
                Edict[a-1] = Edict[a]
                Ebuttons[a-1].configure(text = Ebuttons[a]['text'])

76
77
78
            del Edict[i]
            Ebuttons[i].destroy()
            del Ebuttons[i]
79

80
81
82
            if Econtrol == i:
                Econtrol = Econtrol -1
                Ebuttons[Econtrol]['relief']='sunken'
83
        elem_select(0, Ebuttons)
84

Matheus Müller's avatar
Matheus Müller committed
85
##############################################################################
86
87
# Element Frames

Matheus Müller's avatar
Matheus Müller committed
88
89
    Labelprop = tk.LabelFrame(window, text = 'Properties', relief='raised', bd=2)
    Labelprop.pack(side = 'top')
90
    Label1 = tk.LabelFrame(window, text = 'Elements', relief='raised', bd=2)
Matheus Müller's avatar
Matheus Müller committed
91
    Label1.pack(side = 'top') 
92
    # For Button Creation
93
94
    Eframe = tk.Canvas(Label1)
    Eframe.pack(fill='both',expand=0)
Matheus Müller's avatar
Matheus Müller committed
95

96
97
98
##############################################################################
# Init
    create()
Matheus Müller's avatar
Matheus Müller committed
99
    
100
101
102
##############################################################################
# Properties

103
    global Econtrol, Edict, Labentrys
104
    def update(i):
105
106
107
108
109
110
111
112
113
114
115
116
        Edict[i]['name'] = str(Labentrys[6].get())
        Labentrys[0].configure(text = '%s' %Edict[i]['name'])
        Edict[i]['mass'] = float(Labentrys[7].get())
        Labentrys[1].configure(text = 'Mass - %f' %Edict[i]['mass'])
        Edict[i]['Z'] = float(Labentrys[8].get())
        Labentrys[2].configure(text = 'Z - %f' %Edict[i]['Z'])
        Edict[i]['symbol'] = str(Labentrys[9].get())
        Labentrys[3].configure(text = 'Symbol - %s' %Edict[i]['symbol'])
        Edict[i]['LineShape'] = float(Labentrys[10].get())
        Labentrys[4].configure(text = 'Line Shape α - %f' %Edict[i]['LineShape'])
        Edict[i]['profundidademax'] = float(Labentrys[11].get())
        Labentrys[5].configure(text = 'Max Depht - %f' %Edict[i]['profundidademax'])
Matheus Müller's avatar
Matheus Müller committed
117
        Edict[i]['dist'] = np.loadtxt(Edict[i]['symbol']+".prof")
118

119
120
121
122
123
124
125
126
127
    for i in range(3):
        Frames.insert(i, tk.Frame(Labelprop) )

    Frames[0].pack(side='left')
    Frames[1].pack(side='top')
    Frames[2].pack(side='right')

    for i in arange(3, 5):
        Frames.insert(i, tk.Frame(Frames[0]) )
128

129
130
    Frames[3].pack(side='left')
    Frames[4].pack(side='right')
131

132
133
134
135
136
137
138
139
140
    for i in arange(5, 7):
        Frames.insert(i, tk.Frame(Frames[1]) )

    Frames[5].pack(side='left')
    Frames[6].pack(side='right')
    Labeldist = tk.LabelFrame(Frames[5], text = 'Distribution', relief='raised', bd=2)
    Labeldist.pack(side = 'bottom') 
    Frames.insert(7, tk.Frame(Labeldist) )
    Frames[7].pack(side='top')
141

142
143
144
145
146
147
148
    # Label // Entries numbers:
    # 0 // 6 = Name
    # 1 // 7 = Mass
    # 2 // 8 = Z (Atomic number)
    # 3 // 9 = Symbol
    # 4 // 10 = Line shape
    # 5 // 11 = Max Depht
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
149
    ## 13 // 12 = Resonance energy (For RNA only)
150
151
152
153
154
155
    Labentrys.insert(0, tk.Label(Frames[3], width=23, pady=2, text='%s' %Edict[int(Econtrol)]['name']) )
    Labentrys.insert(1, tk.Label(Frames[3], width=23, pady=2, text='Mass - %f' %Edict[int(Econtrol)]['mass']) )
    Labentrys.insert(2, tk.Label(Frames[3], width=23, pady=2, text='Z - %f' %Edict[int(Econtrol)]['Z']) )
    Labentrys.insert(3, tk.Label(Frames[3], width=23, pady=2, text= 'Symbol - %s' %Edict[int(Econtrol)]['symbol']) )
    Labentrys.insert(4, tk.Label(Frames[3], width=23, pady=2, text='Line Shape α - %f' %Edict[int(Econtrol)]['LineShape']) )
    Labentrys.insert(5, tk.Label(Frames[3], width=23, pady=2, text='Max Depht - %f' %Edict[int(Econtrol)]['profundidademax']) )
156

Matheus Müller's avatar
Ajustes    
Matheus Müller committed
157
    for i in arange(6, 13):
158
        Labentrys.insert(i, tk.Entry(Frames[4], width=17) )
Matheus Müller's avatar
Ajustes    
Matheus Müller committed
159
160
161
162

    Labentrys.insert(13, tk.Label(Frames[3], width=23, pady=2, text='Resonance energy') )

    for i in range(14):
163
164
        Labentrys[i].pack()

165
    # Declaration of interpolation and distribution method variables
166
    methodvar = tk.StringVar()
Matheus Müller's avatar
Matheus Müller committed
167
    methodvar.set('step')
168

169

170
171
172
##############################################################################
# Load/Save sample
    def LOADSAMPLE():
173
174
175
        global i, Econtrol
        arquivo = tkFileDialog.askopenfile()
        for n in arange(9,18):
176
            OFlabentries[n].delete(0,'end')
177
            OFlabentries[n].insert(0,float(arquivo.readline()))
178
179
        for n in arange(21,25):
            OFlabentries[n].delete(0,'end')
180
181
            OFlabentries[n].insert(0,float(arquivo.readline()))
        f = loadtxt(arquivo.name + '.elm', dtype="string")
182
183
184
185
186
187
        els = f[:, 0]
        eln = f[:, 1]
        elm = f[:, 2]
        elz = f[:, 3]
        LSs = f[:, 4]
        eld = f[:, 5]
188
189
190
191
192
193
        Edict.clear()
        for n in range(len(Ebuttons)):
            Ebuttons[n].destroy()
        del Ebuttons[:]
        i = 0
        Econtrol = 0
194
        for n in range(len(els)):
195
            create()           
196
197
198
199
200
201
202
            Edict[n]['LineShape'] = float(LSs[n])
            Edict[n]['symbol'] = str(els[n])
            Edict[n]['name'] = str(eln[n])
            Edict[n]['mass'] = float(elm[n])
            Edict[n]['Z'] = float(elz[n])
            Edict[n]['profundidademax'] = float(eld[n])
            Ebuttons[n]['text'] = str(els[n])
203
204
            shutil.copy(arquivo.name + '-' + Edict[n]['symbol'] + '.dat' , Edict[n]['symbol']+'.dat')
            shutil.copy(arquivo.name + '-' + Edict[n]['symbol'] + '.prof' , Edict[n]['symbol']+'.prof')
205
        elem_select(0,Ebuttons)
206
        arquivo.close()
207
208

    def SAVESAMPLE():
209
210
211
212
        config = tkFileDialog.asksaveasfile(mode='w')
        elist = open(config.name + '.elm', 'w+')
        for k in arange(9,18):
            config.write( (OFlabentries[k].get())+'\n' )
213
        for j in arange(21,25):
214
215
            config.write( (OFlabentries[j].get())+'\n' )
        config.write( '##############################################################################\n' )
216
        config.write( '# dƐ/dx\n# dω²/dx\n# θ out\n# θ in\n# FWHM\n# E min\n# E max\n# E step\n# Depth step\n# Ion energy\n# Ion mass\n# Ion Z\n # Dose\n' )
217
        for n in range(len(Edict)):
218
219
220
221
            elist.write(Edict[n]['symbol']+' ')
            elist.write(Edict[n]['name']+' ')
            elist.write(str(Edict[n]['mass'])+' ')
            elist.write(str(Edict[n]['Z'])+' ')
222
            elist.write(str(Edict[n]['LineShape'])+' ')
223
            elist.write(str(Edict[n]['profundidademax'])+'\n')
224
225
226
            shutil.copy(Edict[n]['symbol']+'.prof', config.name  + '-' + Edict[n]['symbol'] + '.prof')
            shutil.copy(Edict[n]['symbol']+'.dat', config.name  + '-' + Edict[n]['symbol'] + '.dat')
            shutil.copy('temp.sim', config.name  + '.sim')
227
        elist.close()
228
        config.close()
229

230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
##############################################################################
# Menu
    def donothing():
        return 0

    menubar = tk.Menu(mainwindow, bg='grey', relief='flat', bd=0)
    filemenu = tk.Menu(menubar, tearoff=0)
    filemenu.add_command(label="Simulate", command=lambda i=int(Econtrol) :calcbutton.invoke())
    filemenu.add_separator()
    filemenu.add_command(label="Exit", command=sys.exit)
    menubar.add_cascade(label="File", menu=filemenu)

    samplemenu = tk.Menu(menubar, tearoff=0)
    samplemenu.add_command(label="Load Sample", command=lambda i=int(Econtrol) :LOADSAMPLE())
    samplemenu.add_command(label="Save Sample", command=lambda i=int(Econtrol) :SAVESAMPLE())
    menubar.add_cascade(label="Samples", menu=samplemenu)

    helpmenu = tk.Menu(menubar, tearoff=0)
    helpmenu.add_command(label="Help!", command=donothing)
    helpmenu.add_command(label="About...", command=donothing)
    menubar.add_cascade(label="Help", menu=helpmenu)
    mainwindow.config(menu=menubar)

253
254
255
##############################################################################
# Buttons

256
    but = tk.Button(Frames[3], command=create, text='Add element', bd=1, height=1,width=15, pady='4')
257
    but.pack()
258
    butrmv = tk.Button(Frames[4], command=remove, text='Remove element', bd=1, height=1,width=14, pady='4')
259
    butrmv.pack()
260
    butPTE = tk.Button(Frames[5], text='Select from PTE', bd=1, width=15, command=lambda i=i :elem_callback(Econtrol,Edict,Ebuttons) )
261
    butPTE.pack()
262
    BUpdate = tk.Button(Frames[5], text ='Update properties', command=lambda i=int(Econtrol) :update(Econtrol), width=15)
263
264
    BUpdate.pack()

265
##############################################################################
266
267
268
# Distribution
 
    def changestate():
Matheus Müller's avatar
Matheus Müller committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
        BLayer['state']='normal'
        BUpdateD['state']='disabled'
        RS['state']='disabled'
        RL['state']='disabled'
        OFlabentries[9]['state']='disabled'
        OFlabentries[10]['state']='disabled'
        for i in arange(6, 12):
            Labentrys[i]['state']='disabled'
        but['state']='disabled'
        butrmv['state']='disabled'
        butPTE['state']='disabled'
        BUpdate['state']='disabled'
        for n in arange(0, len(Ebuttons)):
            Ebuttons[n]['state'] = 'disabled'
Matheus Müller's avatar
Matheus Müller committed
283
        distvar.set(0)   # Foi preciso forcar isso pois o radiobutton nao o fazia
Matheus Müller's avatar
Matheus Müller committed
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299

    def changestate2():
        BLayer['state']='disabled'
        BUpdateD['state']='normal'
        RS['state']='normal'
        RL['state']='normal'
        OFlabentries[9]['state']='normal'
        OFlabentries[10]['state']='normal'
        for i in arange(6, 12):
            Labentrys[i]['state']='normal'
        but['state']='normal'
        butrmv['state']='normal'
        butPTE['state']='normal'
        BUpdate['state']='normal'
        for n in arange(0, len(Ebuttons)):
            Ebuttons[n]['state'] = 'normal'
Matheus Müller's avatar
Matheus Müller committed
300
        distvar.set(1)    # Foi preciso forcar isso pois o radiobutton nao o fazia
301
302

    def call_layers():
Matheus Müller's avatar
Matheus Müller committed
303
        layers()    # funcao de layers.py
304
305
306
307
308
309
310
311
312

    # By Layers
    BLayer = tk.Button(Labeldist, text ='Open Layers', command = lambda i = int(Econtrol) : call_layers(), width=15)
    BLayer.pack()
    # By Drawing
    BUpdateD = tk.Button(Labeldist, text = 'Element Distribution', command = lambda i = int(Econtrol) : os.system("python profiler.py %s %s %s %s &" %( Edict[int(Econtrol)]['symbol'], str(methodvar.get()), str(Labentrys[11].get()), str(OFlabentries[17].get()) ) ), width=15)
    BUpdateD.pack()
    # Interpolation methods
    RS = tk.Radiobutton(Labeldist, text="Step", variable=methodvar, value='step')
313
    RS.pack(side='left')
314
    RL = tk.Radiobutton(Labeldist, text="Lines", variable=methodvar, value='reta')
315
    RL.pack(side='right')
316
    # Distribution methods
Matheus Müller's avatar
Matheus Müller committed
317
    RLay = tk.Radiobutton(Frames[7], text="Layers", command=lambda i=int(Econtrol) :changestate(), variable=distvar, value=0)
318
    RLay.pack(side='left')
Matheus Müller's avatar
Matheus Müller committed
319
    RDra = tk.Radiobutton(Frames[7], text="Drawing", command=lambda i=int(Econtrol) :changestate2(), variable=distvar, value=1)
320
    RDra.pack(side='right')
321

322
323
324
325
##############################################################################
# Experiment variations

    def changemode():
Matheus Müller's avatar
Matheus Müller committed
326
327
328
329
330
331
332
        for n in range(len(Ebuttons)):
            Ebuttons[n]['state']='normal'
        but['state'] = 'normal'
        butrmv['state'] = 'normal'
        butPTE['state'] = 'normal'
        RLay['state'] = 'normal'
        Labentrys[12]['state'] = 'disabled'
Matheus Müller's avatar
Matheus Müller committed
333
        modevar.set(0)   # Foi preciso forcar isso pois o radiobutton nao o fazia
Matheus Müller's avatar
Matheus Müller committed
334
335
336
337
338
339
340
341
342
343

    def changemode2():
        RDra.invoke()
        elem_select(0,Ebuttons)
        but['state'] = 'disabled'
        butrmv['state'] = 'disabled'
        RLay['state'] = 'disabled'
        Labentrys[12]['state'] = 'normal'
        for n in arange(1, len(Ebuttons)):
            Ebuttons[n]['state'] = 'disabled'
Matheus Müller's avatar
Matheus Müller committed
344
        modevar.set(1)   # Foi preciso forcar isso pois o radiobutton nao o fazia
Matheus Müller's avatar
Matheus Müller committed
345
346

    Rions = tk.Radiobutton(Labelframe, text="Ion Scattering", variable=modevar, value=0, width=18, command=lambda i=i :changemode())
347
    Rions.pack()
Matheus Müller's avatar
Matheus Müller committed
348
    Rrrna = tk.Radiobutton(Labelframe, text="Resonant RNA", variable=modevar, value=1, width=18, command=lambda i=i :changemode2())
349
350
    Rrrna.pack()

Matheus Müller's avatar
Matheus Müller committed
351
##############################################################################
Matheus Müller's avatar
Matheus Müller committed
352
# Init
353

Matheus Müller's avatar
Matheus Müller committed
354
    Rions.invoke()
355
    RDra.invoke()
356
    RS.select()
Matheus Müller's avatar
Matheus Müller committed
357
    Ebuttons[0].invoke()
358

Matheus Müller's avatar
Matheus Müller committed
359
##############################################################################
360