class App: def __init__(self,master): self.master = master self.master.resizable(0,0) self.master.update_idletasks() self.master.overrideredirect(1) self.master.attributes("-topmost",True) self.master.title("New Graph") self.master.lift() self.menubar = Menu(self.master) self.master.config(menu=self.menubar) self.default_color = self.master.cget("bg") self.active_edit_flag = 0 self.modify_flag = 0 self.graph_types = ['Default','Maelstrom','Rankine Half-Body','Rankine Oval', 'Cylinder','Stagnation & Vortex'] #----------------------------------------------------------------------- self.file_menu = Menu(self.menubar) self.Fig = matplotlib.figure.Figure(figsize=(2.148,1.777),dpi=100,tight_layout=True) self.FigSubPlot = self.Fig.add_subplot(111) frame = Frame(master,bg='#%02x%02x%02x' % (231, 231, 231)) frame.pack(fill=BOTH, expand=1) frame2 = Frame(frame,bg='#%02x%02x%02x' % (221, 221, 221)) frame2.pack(fill=BOTH, expand=1,padx=20,pady=23) self.frame3 = Frame(frame2, bg='#%02x%02x%02x' % (221, 221, 221)) self.frame3.pack(fill=BOTH, padx=17,pady=17,expand=1) self.radio_frame = Frame(self.frame3,bg='#%02x%02x%02x' % (221, 221, 221)) #self.listbox = Listbox(self.frame3) self.radio_var = StringVar() self.radio_var.set('Default') for key in self.graph_types: b = Radiobutton(self.radio_frame,text=key,variable=self.radio_var,value = key,bg='#%02x%02x%02x' % (221, 221, 221),indicatoron=1) b.pack(anchor=W) # for key_num in range(0,len(self.graph_types)): # self.listbox.insert(END,self.graph_types[key_num]) # self.listbox.select_set(0) # self.listbox.bind('<<ListboxSelect>>',self.changegraph) self.radio_var.trace('w',self.changegraph) # self.old = self.listbox.get(self.listbox.curselection()) self.blankc = Canvas(self.frame3,width=225,height=188) self.canvas = FigureCanvasTkAgg(self.Fig, master=self.frame3) self.canvas.show() self.canvas.get_tk_widget().pack(side=RIGHT,padx=0,pady=5) self.radio_frame.pack(side=LEFT,anchor=W,padx=8,pady=5,fill=BOTH,expand=1) #self.listbox.pack(side=LEFT,anchor=W,padx=8,pady=5,fill=BOTH,expand=1) self.button_frame = Frame(frame,bg='#%02x%02x%02x' % (231, 231, 231)) self.button_frame.pack(side=RIGHT) self.buffer_frame = Frame(self.button_frame,bg='#%02x%02x%02x' % (231, 231, 231)) self.buffer_frame.pack(side=BOTTOM,pady=8) self.buffer_frame2 = Frame(self.button_frame,bg='#%02x%02x%02x' % (231, 231, 231)) self.buffer_frame2.pack(side=RIGHT,padx=11) self.button_c = Button(self.button_frame,width=9, text='Choose',bg='#%02x%02x%02x' % (231, 231, 231),command=self.Continue) self.button_x = Button(self.button_frame,text='Cancel',bg='#%02x%02x%02x' % (231, 231, 231),width=9,command=self.quit) self.button_c.focus() self.button_c.pack(side=RIGHT) self.button_x.pack(side=RIGHT,padx=14) self.line_color = StringVar() self.line_color.set('#000000') self.wt_var = StringVar() self.wt_var.set('- WT:') self.wt_var.trace('w',self.wt_update) self.linet_var = StringVar() self.linet_var.set('-') self.linet_var.trace('w',self.line_style_update) self.line_var = IntVar() self.line_var.set(25) self.line_val = 25 self.div_check_var = IntVar() self.div_check_var.set(50) self.div_val = 50 self.density_var = IntVar() self.density_var.set(35) self.density_val = 35 self.arrow_var = IntVar() self.arrow_var.set(25) self.arrow_val = 25 self.div_var = IntVar() self.div_var.set(0) self.mark_var = IntVar() self.mark_var.set(0) self.lock_var = IntVar() self.lock_var.set(1) self.active_numbers = {'s':0,'v':0,'u':0,'d':0,'n':0} self.sel_point = None self.selected = None def changegraph(self,*args): self.xlim = [-5.0,5.0] self.ylim = [-5.0,5.0] Y, X = mgrid[-5:5:100j, -5:5:100j] self.X,self.Y = X,Y self.FigSubPlot.clear() #if self.listbox.get(self.listbox.curselection()) == 'Default': if self.radio_var.get() == 'Default': self.U = 0*X self.V = 0*X self.FigSubPlot.streamplot(X,Y,self.U,self.V) #elif self.listbox.get(self.listbox.curselection()) == 'Maelstrom': elif self.radio_var.get() == 'Maelstrom': self.U = self.source(X,Y,1)[0]+self.vortex(X,Y,1)[0] self.V = self.source(X,Y,1)[1]+self.vortex(X,Y,1)[1] self.FigSubPlot.streamplot(X,Y,self.U,self.V, color='k', linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle='-') #elif self.listbox.get(self.listbox.curselection()) == 'Rankine Half-Body': elif self.radio_var.get() == 'Rankine Half-Body': self.U = self.source(X,Y,10)[0]+self.uniform(X,Y,1,1,0)[0] self.V = self.source(X,Y,10)[1]+self.uniform(X,Y,1,1,0)[1] self.FigSubPlot.streamplot(X, Y,self.U,self.V, color='k', linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle='-') #elif self.listbox.get(self.listbox.curselection()) == 'Rankine Oval': elif self.radio_var.get() == 'Rankine Oval': self.U = self.source(X+2,Y,10)[0]+self.source(X-2,Y,-10)[0]+self.uniform(X,Y,1,1,0)[0] self.V = self.source(X+2,Y,10)[1]+self.source(X-2,Y,-10)[1]+self.uniform(X,Y,1,1,0)[1] self.FigSubPlot.streamplot(X, Y,self.U,self.V, color='k', linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle='-') #elif self.listbox.get(self.listbox.curselection()) == 'Cylinder': elif self.radio_var.get() == 'Cylinder': self.U = self.doublet(X,Y,25)[0]+self.uniform(X,Y,1,1,0)[0] self.V = self.doublet(X,Y,25)[1]+self.uniform(X,Y,1,1,0)[1] self.FigSubPlot.streamplot(X, Y,self.U,self.V, color='k', linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle='-') #elif self.listbox.get(self.listbox.curselection()) == 'Stagnation & Vortex': elif self.radio_var.get() == 'Stagnation & Vortex': self.U = self.vortex(X,Y,25)[0]+self.corner(X,Y,'2,1')[0] self.V = self.vortex(X,Y,25)[1]+self.corner(X,Y,'2,1')[1] self.FigSubPlot.streamplot(X, Y,self.U,self.V, color='k', linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle='-') self.FigSubPlot.set_xlim(self.xlim) self.FigSubPlot.set_ylim(self.ylim) self.canvas.draw() # self.old = self.listbox.get(self.listbox.curselection()) def source(self,X,Y,l): l = float(l) U = (l/(2*pi))*X/(X*X + Y*Y) V = (l/(2*pi))*Y/(X*X + Y*Y) return (U,V) def vortex(self,X,Y,g): g = float(g) U = (g/(2*pi))*Y/(X*X + Y*Y) V = (g/(2*pi))*-X/(X*X + Y*Y) return (U,V) def uniform(self,X,Y,v_0,x_0,y_0): v_0 = float(v_0) x_0 = float(x_0) y_0 = float(y_0) U = v_0*x_0/(x_0*x_0+y_0*y_0)**0.5 V = v_0*y_0/(x_0*x_0+y_0*y_0)**0.5 return (U,V) def doublet(self,X,Y,k): k = float(k) U = (k/(2*pi))*((2*Y*Y/((X*X+Y*Y)*(X*X+Y*Y)))-(1/(X*X+Y*Y))) V = -(k/(2*pi))*(2*X*Y/((X*X+Y*Y)*(X*X+Y*Y))) return (U,V) def corner(self,X,Y,tup): comma_flag = 0 A = '' n = '' for char in tup: if char == ',': comma_flag = 1 elif comma_flag == 0: n += char elif comma_flag == 1: A += char A = float(A) n = float(n) R = (X*X+Y*Y)**0.5 t = arctan2(-Y,-X) U = -A*n*R**(n-1)*(cos(n*t)*cos(t)+sin(n*t)*sin(t)) V = -A*n*R**(n-1)*(cos(n*t)*sin(t)-sin(n*t)*cos(t)) return (U,V) def stream_source(self,X,Y,l): l = float(l) stream = (l/(2*pi))*arctan2(-Y,-X) return stream def stream_vortex(self,X,Y,g): g = float(g) stream = (g/(2*pi))*log((X*X + Y*Y)**0.5) return stream def stream_uniform(self,X,Y,v_0,x_0,y_0): v_0 = float(v_0) x_0 = float(x_0) y_0 = float(y_0) stream = (v_0*Y*x_0/(x_0*x_0+y_0*y_0)**0.5)-(v_0*X*y_0/(x_0*x_0+y_0*y_0)**0.5) return stream def stream_doublet(self,X,Y,k): k = float(k) stream = -(k/(2*pi))*Y/(X*X+Y*Y) return stream def stream_corner(self,X,Y,tup): comma_flag = 0 A = '' n = '' for char in tup: if char == ',': comma_flag = 1 elif comma_flag == 0: n += char elif comma_flag == 1: A += char A = float(A) n = float(n) stream = A*(X*X+Y*Y)**(n*0.5)*sin(n*arctan2(-Y,-X)) return stream def quit(self): self.master.destroy() def Continue(self): self.master.withdraw() self.main = Toplevel(self.master) self.main.geometry("%dx%d+%d+%d" % (1038-206, 694, int((500.0/2560.0)*screen_resolution[0]), int((60.0/1440.0)*screen_resolution[1]))) self.main.minsize(376,227) self.interior = PanedWindow(self.main,sashwidth=5) self.interior.pack(fill=BOTH, expand=1) self.elements_frame = Frame(self.interior, height=1038, width=212,relief=RIDGE,borderwidth=0) self.interior.add(self.elements_frame) self.interior.paneconfig(self.elements_frame,minsize=130) self.graph_frame = Frame(self.interior) self.interior.add(self.graph_frame) self.interior.paneconfig(self.graph_frame,minsize=130) self.main.bind("<ButtonRelease-1>",self.pan_update) self.main.bind("<ButtonRelease-3>",self.pan_update) self.main.bind("<Button-3>",self.right_menu) self.edit_frame = Frame(self.graph_frame) self.edit_frame.pack(side=TOP,fill=X,padx=10) self.canvas = FigureCanvasTkAgg(self.Fig, master=self.graph_frame) self.canvas.show() self.canvas.get_tk_widget().pack(fill=BOTH,expand=1) self.nav_frame = Frame(self.edit_frame) self.nav_frame.pack(side=LEFT,anchor=W) self.toolbar = NavigationToolbar(self.canvas, self.nav_frame) self.main.protocol('WM_DELETE_WINDOW',self.master.destroy) self.elements = Treeview(self.elements_frame,columns=("values","xlocations","ylocations"),selectmode=BROWSE) self.elements.heading("#0",text="Components") self.elements.heading("values",text="Str") self.elements.heading("xlocations",text="X") self.elements.heading("ylocations",text="Y") self.elements.column("#0",width=90,anchor=CENTER) self.elements.column("values",width=45,anchor=CENTER) self.elements.column("xlocations",width=26,anchor=CENTER) self.elements.column("ylocations",width=26,anchor=CENTER) self.elements.pack(fill=BOTH,expand=1) self.as_button_frame = Frame(self.elements_frame) self.add_button = Button(self.as_button_frame,text = '+',width=2,command=self.add) self.sub_button = Button(self.as_button_frame,text = '-',width=2,command=self.subtract) self.options_button = Button(self.as_button_frame,text='Options',command=self.options) self.as_button_frame.pack(side=BOTTOM,anchor=W) self.options_button.pack(side=RIGHT) self.sub_button.pack(side=RIGHT,anchor=W) self.add_button.pack(side=LEFT,anchor=W) self.addsub_menu = Menu(self.as_button_frame,tearoff=0) self.addsub_menu.add_command(command=self.add_source,label='Source') self.addsub_menu.add_command(command=self.add_vortex,label='Vortex') self.addsub_menu.add_command(command=self.add_uniform,label='Uniform') self.addsub_menu.add_command(command=self.add_doublet,label='Doublet') self.addsub_menu.add_command(command=self.add_corner,label='Corner') #if self.listbox.get(self.listbox.curselection()) == 'Default': if self.radio_var.get() == 'Default': self.active_components = [] self.active_calls = {} pass #elif self.listbox.get(self.listbox.curselection()) == 'Maelstrom': elif self.radio_var.get() == 'Maelstrom': self.elements.insert("",0,"M",text="Maelstrom",open=TRUE) self.active_components = ['M'] self.elements.insert("M",0,iid='s%s'%self.active_numbers['s'],text="Source",values=("%s"%1,0,0)) self.elements.insert("M",0,iid='v%s'%self.active_numbers['v'],text="Vortex",values=("%s"%1,0,0)) self.active_calls = {'s%s'%self.active_numbers['s']:("s",self.elements.item('s%s'%self.active_numbers['s'],"values")), 'v%s'%self.active_numbers['v']:("v",self.elements.item('v%s'%self.active_numbers['v'],"values"))} self.active_numbers['s'] += 1 self.active_numbers['v'] += 1 elif self.radio_var.get() == 'Rankine Half-Body': self.elements.insert("",0,"RHF",text="Rankine Half-Body",open=TRUE) self.active_components = ['RHF'] self.elements.insert("RHF",0,iid='s%s'%self.active_numbers['s'],text="Source",values=("%s"%10,0,0)) self.elements.insert("RHF",0,iid='u%s'%self.active_numbers['u'],text="Uniform",values=("%s"%1,1,0)) self.active_calls = {'s%s'%self.active_numbers['s']:("s",self.elements.item('s%s'%self.active_numbers['s'],"values")), 'u%s'%self.active_numbers['u']:("u",self.elements.item('u%s'%self.active_numbers['u'],"values"))} self.active_numbers['s'] += 1 self.active_numbers['u'] += 1 elif self.radio_var.get() == 'Rankine Oval': self.elements.insert("",0,"RO",text="Rankine Oval",open=TRUE) self.active_components = ['RO'] self.elements.insert("RO",0,iid='s%s'%self.active_numbers['s'],text="Source",values=("%s"%10,-2,0)) self.elements.insert("RO",0,iid='s%s'%(self.active_numbers['s']+1),text="Source",values=("%s"%-10,2,0)) self.elements.insert("RO",0,iid='u%s'%self.active_numbers['u'],text="Uniform",values=("%s"%1,1,0)) self.active_calls = {'s%s'%self.active_numbers['s']:("s",self.elements.item('s%s'%self.active_numbers['s'],"values")), 's%s'%(self.active_numbers['s']+1):("s",self.elements.item('s%s'%(self.active_numbers['s']+1),"values")), 'u%s'%self.active_numbers['u']:("u",self.elements.item('u%s'%self.active_numbers['u'],"values"))} self.active_numbers['s'] += 2 self.active_numbers['u'] += 1 elif self.radio_var.get() == 'Cylinder': self.elements.insert("",0,'D+U',text="Cylinder",open=TRUE) self.active_components = ['D+U'] self.elements.insert('D+U',0,iid='d%s'%self.active_numbers['d'],text="Doublet",values=("%s"%25,0,0)) self.elements.insert('D+U',0,iid='u%s'%self.active_numbers['u'],text="Uniform",values=("%s"%1,1,0)) self.active_calls = {'d%s'%self.active_numbers['d']:("d",self.elements.item('d%s'%self.active_numbers['d'],"values")), 'u%s'%self.active_numbers['u']:("u",self.elements.item('u%s'%self.active_numbers['u'],"values"))} self.active_numbers['d'] += 1 self.active_numbers['u'] += 1 elif self.radio_var.get() == 'Stagnation & Vortex': self.elements.insert("",0,'S+V',text="Stag+Vort",open=TRUE) self.active_components = ['S+V'] self.elements.insert('S+V',0,iid='n%s'%self.active_numbers['n'],text="C (n,A)",values=("%s,%s"%(2,1),0,0)) self.elements.insert('S+V',0,iid='v%s'%self.active_numbers['v'],text="Vortex",values=("%s"%25,0,0)) self.active_calls = {'n%s'%self.active_numbers['n']:("n",self.elements.item('n%s'%self.active_numbers['n'],"values")), 'v%s'%self.active_numbers['v']:("v",self.elements.item('v%s'%self.active_numbers['v'],"values"))} self.active_numbers['n'] += 1 self.active_numbers['v'] += 1 self.elements.bind("<Double-Button-1>",self.edit) self.elements.bind('<<TreeviewSelect>>',self.treeview_select) self.main.bind("<Return>",self.edit_return) self.main.bind("<Escape>",self.edit_return) self.rightc_menu = Menu(self.graph_frame,tearoff=0) self.add_menu = Menu(self.rightc_menu,tearoff=0) self.rightc_menu.add_cascade(label="Add",menu=self.add_menu) self.add_menu.add_command(command=self.addm_source,label='Source') self.add_menu.add_command(command=self.addm_vortex,label='Vortex') self.add_menu.add_command(command=self.addm_uniform,label='Uniform') self.add_menu.add_command(command=self.addm_doublet,label='Doublet') self.add_menu.add_command(command=self.addm_corner,label='Corner') def right_menu(self,event): print "event", event.x,event.y #print "graph", self.main.winfo_x()+self.interior.winfo_x()+self.graph_frame.winfo_x(), self.main.winfo_y()+self.interior.winfo_y()+self.graph_frame.winfo_y() self.plot_x,self.plot_y = self.FigSubPlot.transData.inverted().transform((event.x,event.y+20)) self.plot_y = -self.plot_y self.rightc_menu.post(self.main.winfo_x()+self.interior.winfo_x()+self.graph_frame.winfo_x()+event.x+9,self.main.winfo_y()+self.graph_frame.winfo_y()+event.y+66) def treeview_select(self,event): if self.elements.get_children(self.elements.selection()[0]) == (): if self.mark_var.get() == 0 or self.selected == self.elements.selection(): pass elif self.mark_var.get() == 1: #add red markers here if self.elements.selection() != '': child = self.elements.selection()[0] if child != '': self.sel_point = (self.elements.item(child,"values")[1],self.elements.item(child,"values")[2]) self.graph_update() self.selected = self.elements.selection() else: pass else: if self.mark_var.get() == 0 or self.selected == self.elements.selection(): pass elif self.mark_var.get() == 1: if self.elements.selection() != '': child = self.elements.selection()[0] if child != '': self.sel_point = None self.graph_update() self.selected = self.elements.selection() def mark_check_fun(self): if self.mark_var.get() == 0: self.sel_point = None self.graph_update() elif self.elements.selection() != '': child = self.elements.selection()[0] self.sel_point = [self.elements.item(child,"values")[1],self.elements.item(child,"values")[2]] self.graph_update() def add(self): self.addsub_menu.post(self.main.winfo_x()+self.as_button_frame.winfo_x()+self.add_button.winfo_x(),self.main.winfo_y()+self.as_button_frame.winfo_y()+self.add_button.winfo_y()-len(self.active_numbers.keys())*14) def subtract(self): if self.active_edit_flag == 1: self.del_edit(self) child = self.elements.selection()[0] if child == '': return if self.elements.parent(child) == '': ID = child self.active_components.remove(child) self.elements.delete(child) else: ID = self.elements.parent(child) self.active_components.remove(ID) for comp in self.elements.get_children(ID): if comp != child: self.active_components.append(comp) self.elements.move(comp,"",0) self.elements.delete(ID) self.sel_point = None self.graph_update() def options(self): self.options_window = Toplevel(self.main) self.options_window.geometry("%dx%d+%d+%d" % (280, 200+20, self.main.winfo_x()+self.as_button_frame.winfo_x()+self.options_button.winfo_x()-272+26+72+86, self.main.winfo_y()+self.as_button_frame.winfo_y()+self.options_button.winfo_y()-196-20)) self.main.bind('<FocusIn>',self.close_options) self.options_window.title("Options") self.options_window.update_idletasks() self.options_window.bind("<ButtonRelease-1>",self.pan_update) self.options_frame = Frame(self.options_window) self.options_frame.pack(fill=BOTH,expand=1) self.options_window.attributes("-topmost",True) self.line_frame = Frame(self.options_frame,bd=2,relief=RIDGE) self.line_frame.grid(row=0,column=1,sticky=N+S+E+W,ipady=7) self.color_frame = Frame(self.options_frame,bd=2,relief=RIDGE,padx=2) self.color_frame.grid(row=0,column=0,sticky=N+S+E+W) self.color_button = Label(self.color_frame,bg=self.line_color.get(),text = ' ') self.color_button.pack(side=RIGHT) Label(self.color_frame,text='C:').pack(side=RIGHT) self.color_button.bind('<Button-1>',self.getColor) self.wt_slider = Scale(self.line_frame,from_=1,to=100,orient=HORIZONTAL,variable=self.line_var) if self.wt_var.get() == '-> WT:': self.wt_slider.config(variable=self.arrow_var) elif self.wt_var.get() == '- WT:': self.wt_slider.config(variable=self.line_var) elif self.wt_var.get() == 'div WT:': self.wt_slider.config(variable=self.div_check_var) self.wt_slider.pack(side=RIGHT) self.wt_menu = OptionMenu(self.line_frame,self.wt_var,'- WT:','-> WT:','div WT:') self.wt_menu.pack(side=RIGHT,anchor='center',fill=X,expand=1) self.wt_menu.config(width=8) self.density_frame = Frame(self.options_frame,bd=2,relief=RIDGE) self.density_frame.grid(row=1,column=1,sticky=N+S+E+W) self.density_slider = Scale(self.density_frame,from_=1,to=100,orient=HORIZONTAL,variable=self.density_var) self.density_slider.pack(side=RIGHT) Label(self.density_frame,text='Density').pack(side=LEFT,anchor=CENTER,fill=X,expand=1) self.linet_frame = Frame(self.options_frame,bd=2,relief=RIDGE) self.linet_frame.grid(row=1,column=0,sticky=N+S+E+W) Label(self.linet_frame,text='Style').pack(anchor='n') self.linet_menu = OptionMenu(self.linet_frame,self.linet_var,'-','->','-|>') self.linet_menu.pack(anchor='s',side=BOTTOM) self.linet_menu.config(width=5) self.div_check = Checkbutton(self.options_frame,text = 'Dividing Streamline',variable=self.div_var,onvalue=1,offvalue=0,command = self.graph_update) self.div_check.grid(row=2,column=1,sticky=W) self.mark_check = Checkbutton(self.options_frame,text = 'Selection Marker',variable=self.mark_var,onvalue=1,offvalue=0,command = self.mark_check_fun) self.mark_check.grid(row=3,column=1,sticky=W) self.limit_frame = Frame(self.options_frame) self.limit_frame.grid(row=4,column=1,sticky=W) #self.aspect_lock = Checkbutton(self.limit_frame,text='LK',variable = self.lock_var) #self.aspect_lock.grid(row=0,column=4,rowspan=2,sticky=E) Label(self.limit_frame,text='x').grid(row=0,column=0) self.xlimlow_var = DoubleVar() self.xlimlow_var.set(self.FigSubPlot.get_xlim()[0]) self.xlimlow_entry = Entry(self.limit_frame,textvariable=self.xlimlow_var,width=5) self.xlimlow_entry.grid(row=0,column=1) Label(self.limit_frame,text='...').grid(row=0,column=2) self.xlimhigh_var = DoubleVar() self.xlimhigh_var.set(self.FigSubPlot.get_xlim()[1]) self.xlimhigh_entry = Entry(self.limit_frame,textvariable=self.xlimhigh_var,width=5) self.xlimhigh_entry.grid(row=0,column=3) Label(self.limit_frame,text='y').grid(row=1,column=0) self.ylimlow_var = DoubleVar() self.ylimlow_var.set(self.FigSubPlot.get_ylim()[0]) self.ylimlow_entry = Entry(self.limit_frame,textvariable=self.ylimlow_var,width=5) self.ylimlow_entry.grid(row=1,column=1) Label(self.limit_frame,text='...').grid(row=1,column=2) self.ylimhigh_var = DoubleVar() self.ylimhigh_var.set(self.FigSubPlot.get_ylim()[1]) self.ylimhigh_entry = Entry(self.limit_frame,textvariable=self.ylimhigh_var,width=5) self.ylimhigh_entry.grid(row=1,column=3) #self.limit_frame.bind('<FocusOut>',self.limits_update) self.xlimhigh_var.trace('w',self.limits_update) self.ylimhigh_var.trace('w',self.limits_update) self.xlimlow_var.trace('w',self.limits_update) self.ylimlow_var.trace('w',self.limits_update) def limits_update(self,*args): if self.xlimlow_entry.get() == '' or self.ylimlow_entry.get() == '' or self.xlimhigh_entry.get() == '' or self.ylimhigh_entry.get() == '': pass elif self.xlimlow_entry.get() == '-' or self.ylimlow_entry.get() == '-' or self.xlimhigh_entry.get() == '-' or self.ylimhigh_entry.get() == '-': pass elif self.xlimlow_entry.get() == '.' or self.ylimlow_entry.get() == '.' or self.xlimhigh_entry.get() == '.' or self.ylimhigh_entry.get() == '.': pass else: self.xlim = [float(self.xlimlow_var.get()),float(self.xlimhigh_var.get())] self.ylim = [float(self.ylimlow_var.get()),float(self.ylimhigh_var.get())] self.FigSubPlot.set_xlim(self.xlim) self.FigSubPlot.set_ylim(self.ylim) self.graph_update() def getColor(self,event): color=askcolor(self.line_color.get()) if color != "None": self.line_color.set(color[1]) self.graph_update() def wt_update(self,*args): if self.wt_var.get() == '-> WT:': self.wt_slider.config(variable=self.arrow_var) elif self.wt_var.get() == '- WT:': self.wt_slider.config(variable=self.line_var) elif self.wt_var.get() == 'div WT:': self.wt_slider.config(variable=self.div_check_var) def line_style_update(self,*args): self.graph_update() def close_options(self,event): self.options_window.destroy() def add_source(self): if self.modify_flag == 0: self.plot_x,self.plot_y = 0,0 self.elements.insert("",0,iid='s%s'%self.active_numbers['s'],text="Source",values=("%s"%1,self.plot_x,self.plot_y)) self.active_calls['s%s'%self.active_numbers['s']] = ("s",self.elements.item('s%s'%self.active_numbers['s'],"values")) self.active_components.append('s%s'%self.active_numbers['s']) if self.modify_flag == 1: self.elements.selection_set('s%s'%self.active_numbers['s']) self.modify_flag = 0 self.graph_update() self.active_numbers['s'] += 1 def add_vortex(self): if self.modify_flag == 0: self.plot_x,self.plot_y = 0,0 self.elements.insert("",0,iid='v%s'%self.active_numbers['v'],text="Vortex",values=("%s"%1,self.plot_x,self.plot_y)) self.active_calls['v%s'%self.active_numbers['v']] = ("v",self.elements.item('v%s'%self.active_numbers['v'],"values")) self.active_components.append('v%s'%self.active_numbers['v']) if self.modify_flag == 1: self.elements.selection_set('v%s'%self.active_numbers['v']) self.modify_flag = 0 self.graph_update() self.active_numbers['v'] += 1 def add_uniform(self): if self.modify_flag == 0: self.plot_x,self.plot_y = 1,0 self.elements.insert("",0,iid='u%s'%self.active_numbers['u'],text="Uniform",values=("%s"%1,self.plot_x,self.plot_y)) self.active_calls['u%s'%self.active_numbers['u']] = ("u",self.elements.item('u%s'%self.active_numbers['u'],"values")) self.active_components.append('u%s'%self.active_numbers['u']) if self.modify_flag == 1: self.elements.selection_set('u%s'%self.active_numbers['u']) self.modify_flag = 0 self.graph_update() self.active_numbers['u'] += 1 def add_doublet(self): if self.modify_flag == 0: self.plot_x,self.plot_y = 0,0 self.elements.insert("",0,iid='d%s'%self.active_numbers['d'],text="Doublet",values=("%s"%1,self.plot_x,self.plot_y)) self.active_calls['d%s'%self.active_numbers['d']] = ("d",self.elements.item('d%s'%self.active_numbers['d'],"values")) self.active_components.append('d%s'%self.active_numbers['d']) if self.modify_flag == 1: self.elements.selection_set('d%s'%self.active_numbers['d']) self.modify_flag = 0 self.graph_update() self.active_numbers['d'] += 1 def add_corner(self): if self.modify_flag == 0: self.plot_x,self.plot_y = 0,0 self.elements.insert("",0,iid='n%s'%self.active_numbers['n'],text="Corner (n,A)",values=("%s,%s"%(2,1),self.plot_x,self.plot_y)) self.active_calls['n%s'%self.active_numbers['n']] = ("n",self.elements.item('n%s'%self.active_numbers['n'],"values")) self.active_components.append('n%s'%self.active_numbers['n']) if self.modify_flag == 1: self.elements.selection_set('n%s'%self.active_numbers['n']) self.modify_flag = 0 self.graph_update() self.active_numbers['n'] += 1 def addm_source(self): self.modify_flag = 1 self.add_source() def addm_vortex(self): self.modify_flag = 1 self.add_vortex() def addm_uniform(self): self.modify_flag = 1 self.add_uniform() def addm_doublet(self): self.modify_flag = 1 self.add_doublet() def addm_corner(self): self.modify_flag = 1 self.add_corner() def pan_update(self,event): if [self.FigSubPlot.get_xlim()[0],self.FigSubPlot.get_xlim()[1]] != self.xlim or [self.FigSubPlot.get_ylim()[0],self.FigSubPlot.get_ylim()[1]] != self.ylim or self.density_var.get() != self.density_val or self.line_var.get() != self.line_val or self.arrow_var.get() != self.arrow_val or self.arrow_var.get() != self.arrow_val or self.div_check_var.get() != self.div_val: self.graph_update() self.density_val = self.density_var.get() self.line_val = self.line_var.get() self.arrow_val = self.arrow_var.get() self.div_val = self.div_check_var.get() else: return def graph_update(self): self.FigSubPlot.clear() Y, X = mgrid[self.FigSubPlot.get_ylim()[0]:self.FigSubPlot.get_ylim()[1]:100j, self.FigSubPlot.get_xlim()[0]:self.FigSubPlot.get_xlim()[1]:100j] self.xlim = [self.FigSubPlot.get_xlim()[0],self.FigSubPlot.get_xlim()[1]] self.ylim = [self.FigSubPlot.get_ylim()[0],self.FigSubPlot.get_ylim()[1]] self.U = 0*X self.V = 0*X self.stream = 0*X for ID in self.active_components: if self.elements.get_children(ID) == (): child = ID self.active_calls[child] = (child[0],self.elements.item(child,"values")) if self.active_calls[child][0] == "s": self.U += self.source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'v': self.U += self.vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'u': if self.active_calls[child][1][1] == '0' and self.active_calls[child][1][2] == '0': pass else: self.U += self.uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2])[0] self.V += self.uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2])[1] self.stream += self.stream_uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2]) elif self.active_calls[child][0] == 'd': self.U += self.doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'n': self.U += self.corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) else: for child in self.elements.get_children(ID): self.active_calls[child] = (child[0],self.elements.item(child,"values")) if self.active_calls[child][0] == "s": self.U += self.source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_source(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'v': self.U += self.vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_vortex(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'u': if self.active_calls[child][1][1] == '0' and self.active_calls[child][1][2] == '0': pass else: self.U += self.uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2])[0] self.V += self.uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2])[1] self.stream += self.stream_uniform(X,Y,self.active_calls[child][1][0],self.active_calls[child][1][1],self.active_calls[child][1][2]) elif self.active_calls[child][0] == 'd': self.U += self.doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_doublet(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) elif self.active_calls[child][0] == 'n': self.U += self.corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[0] self.V += self.corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0])[1] self.stream += self.stream_corner(X-float(self.active_calls[child][1][1]),Y-float(self.active_calls[child][1][2]),self.active_calls[child][1][0]) self.FigSubPlot.streamplot(X,Y,self.U,self.V,color=self.line_color.get(), linewidth=(2.0/71.0)*self.line_var.get()+(13.0/71.0),density=(2.0/71.0)*self.density_var.get()+(13.0/71.0),arrowstyle=self.linet_var.get(),arrowsize=(4.0/71.0)*self.arrow_var.get()+(13.0/71.0)) if self.div_var.get() == 1: #self.FigSubPlot.contour(X,Y,self.stream,[-0.01,0.01],linewidths=[(4.0/71.0)*self.div_check_var.get()+(13.0/71.0),(4.0/71.0)*self.div_check_var.get()+(13.0/71.0)]) self.FigSubPlot.contour(X,Y,self.stream,[0],linewidths=[(4.0/71.0)*self.div_check_var.get()+(13.0/71.0)]) if self.mark_var.get() == 1: if self.sel_point != None: self.plot_point() self.FigSubPlot.set_xlim(self.xlim) self.FigSubPlot.set_ylim(self.ylim) self.canvas.draw() def plot_point(self): if self.elements.selection()[0][0] == 'u': norm = float(float(self.sel_point[0])*float(self.sel_point[0])+float(self.sel_point[1])*float(self.sel_point[1]))**0.5 if norm == 0: norm = 1 X,Y,U,V = 0,0, float(self.sel_point[0])/norm,float(self.sel_point[1])/norm self.FigSubPlot.quiver(X,Y,U,V,angles='xy',scale_units='xy',scale=1,color='g') else: self.FigSubPlot.plot([self.sel_point[0]],[self.sel_point[1]],'r^',ms=10) def edit(self,event): if self.active_edit_flag == 1 or self.elements.identify_row(event.y) == '': pass else: self.rowid = self.elements.identify_row(event.y) self.column = self.elements.identify_column(event.x) self.edit_var = StringVar() if int(self.column[-1]) == 0: self.edit_var.set('%s'%self.elements.item("%s"%self.elements.identify("item",event.x, event.y))['text']) else: self.edit_var.set('%s'%self.elements.item("%s"%self.elements.identify("item",event.x, event.y))['values'][int(self.column[-1])-1]) x,y,width,height = self.elements.bbox(self.rowid, self.column) self.edit_entry = Entry(self.elements_frame,textvariable=self.edit_var) self.edit_entry.place(x=x,y=y,width=width) self.edit_entry.focus_force() self.edit_entry.bind("<FocusOut>", self.del_edit) self.active_edit_flag = 1 def edit_return(self,event): self.main.focus() def del_edit(self,event): if self.column[-1] == '0': self.elements.item(self.rowid,text='%s'%self.edit_var.get()) elif self.rowid[0] == 'n' and self.column[-1] == '1' and ',' not in self.edit_var.get(): pass else: value = '' initial_value = str(self.elements.item(self.rowid)['values'][int(self.column[-1])-1]) comma_flag = 0 div_flag = 0 for index in range(0,len(self.edit_var.get())): char = self.edit_var.get()[index] if char in '-0123456789.': value += char elif self.rowid[0] == 'n' and char == ',': if comma_flag == 1: self.edit_entry.destroy() self.active_edit_flag = 0 return value += char comma_flag = 1 comma_index = index if char == '/': if div_flag == 0: div_flag = 1 div_index = index elif div_flag == 1 and comma_flag == 1: div_flag = 2 div_index2 = index elif div_flag == 1 and comma_flag == 0 or div_flag == 2: self.edit_entry.destroy() self.active_edit_flag = 0 return if div_flag == 1: if comma_flag == 1: if div_index < comma_index: arg = int(float(value[:div_index])*100/float(value[div_index:comma_index-1]))/100.0 value = str(arg)+value[comma_index-1:] elif div_index > comma_index: arg = int(float(value[comma_index+1:div_index])*100/float(value[div_index:]))/100.0 value = value[:comma_index+1]+str(arg) else: arg = int(float(value[:div_index])*100/float(value[div_index:]))/100.0 value = str(arg) elif div_flag == 2: arg1 = int(float(value[:div_index])*100/float(value[div_index:comma_index-1]))/100.0 arg2 = int(float(value[comma_index:div_index2-1])*100/float(value[div_index2-1:]))/100.0 value = str(arg1)+','+str(arg2) if value == '' or value == initial_value: self.edit_entry.destroy() self.active_edit_flag = 0 return self.elements.set(self.rowid,column=(int(self.column[-1])-1),value=value) if self.mark_var.get() == 1: self.sel_point = [self.elements.item(self.rowid,"values")[1],self.elements.item(self.rowid,"values")[2]] self.graph_update() self.edit_entry.destroy() self.active_edit_flag = 0
class Example(Frame): def __init__(self): Frame.__init__(self) self.style = Style() self.style.theme_use("default") self.master.title("Log viewer") self.pack(fill=BOTH, expand=True) self.used = [] # List of currently plotted series ([str]) self.series = {} # List of all series ({str -> [number]}) self.names = [] # List of all nodes in tree view ([str]) self.queues = [] # List of all queues ([str]) self.logs = {} # List of all text logs ({str -> [str]}) self.rowconfigure(1, weight=1) self.columnconfigure(6, weight=3) self.columnconfigure(11, weight=1) # Series selection takes row 1-2, col 0-2 self.series_ui = Treeview(self) self.series_ui.grid(row=1, column=0, columnspan=2, rowspan=2, sticky=N+S) self.series_ui.configure(show="tree") self.series_ui.bind("<Double-Button-1>", self.onselect) self.series_ui.tag_configure("graphall", foreground="#070") self.series_ui.tag_configure("graphnone", foreground="#000") self.series_ui.tag_configure("graphsome", foreground="#007") series_ui_scroll = Scrollbar(self, command=self.series_ui.yview, orient=VERTICAL) series_ui_scroll.grid(row=1, column=2, rowspan=2, sticky=N+S) self.series_ui["yscrollcommand"] = series_ui_scroll.set # The plot takes row 1-2, col 3-6 move_mode = StringVar() move_mode.set("pan") show_path = IntVar() show_path.set(0) event_bars = IntVar() event_bars.set(1) show_debug = IntVar() show_debug.set(1) show_error = IntVar() show_error.set(1) show_warning = IntVar() show_warning.set(1) show_info = IntVar() show_info.set(1) self.plot = HackPlot(self, move_mode, show_path, event_bars, [(show_debug, "[DEBUG]"), (show_error, "[ERROR]"), (show_warning, "[WARNING]"), (show_info, "[INFO]")]) self.plot.canvas.grid(row=1, column=3, columnspan=4, rowspan=2, sticky=N+S+E+W) # Text logs take row 1-2, col 7-12 self.plot.listbox.grid(row=1, column=7, columnspan=5, sticky=N+S+E+W) listbox_yscroll = Scrollbar(self, command=self.plot.listbox.yview, orient=VERTICAL) listbox_yscroll.grid(row=1, column=12, sticky=N+S) self.plot.listbox["yscrollcommand"] = listbox_yscroll.set listbox_xscroll = Scrollbar(self, command=self.plot.listbox.xview, orient=HORIZONTAL) listbox_xscroll.grid(row=2, column=7, columnspan=5, sticky=E+W) self.plot.listbox["xscrollcommand"] = listbox_xscroll.set # Controls take row 0, col 0-12 Button(self, text="Load Directory", command=self.loaddir).grid(row=0, column=0) Button(self, text="Load File", command=self.loadfile).grid(row=0, column=1) Button(self, text="Fit X", command=self.plot.fit_x).grid(row=0, column=3, sticky=W) Button(self, text="Fit Y", command=self.plot.fit_y).grid(row=0, column=4, sticky=W) Button(self, text="Fit Auto", command=self.plot.fit_auto).grid(row=0, column=5, sticky=W) Button(self, text="Fit Tele", command=self.plot.fit_tele).grid(row=0, column=6, sticky=W) # Plot controls in a subframe to manage padding so it doesn't look awful move_mode_control = Frame(self, padx=10) Radiobutton(move_mode_control, text="Pan", value="pan", variable=move_mode).grid(row=0, column=0, sticky=W) Radiobutton(move_mode_control, text="Zoom In", value="zoomin", variable=move_mode).grid(row=0, column=1, sticky=W) Radiobutton(move_mode_control, text="Zoom Out", value="zoomout", variable=move_mode).grid(row=0, column=2, sticky=W) move_mode_control.grid(row=0, column=7, sticky=W) Checkbutton(self, text="Event Bars", variable=event_bars, command=self.plot.show_textlogs).grid(row=0, column=8, sticky=W) Checkbutton(self, text="Debug", variable=show_debug, command=self.plot.show_textlogs).grid(row=0, column=9, sticky=W) Checkbutton(self, text="Error", variable=show_error, command=self.plot.show_textlogs).grid(row=0, column=10, sticky=W) Checkbutton(self, text="Warning", variable=show_warning, command=self.plot.show_textlogs).grid(row=0, column=11, sticky=W) Checkbutton(self, text="Info", variable=show_info, command=self.plot.show_textlogs).grid(row=0, column=12, sticky=W) Checkbutton(self, text="Directories", variable=show_path, command=self.plot.show_textlogs).grid(row=0, column=13, sticky=E) # Open Directory button clicked def loaddir(self): dirname = tkFileDialog.askdirectory(initialdir=log_dir) if dirname != "" and dirname != (): # Cancel and (x) not pressed # Empty the data self.used = [] self.series = {} self.names = [] self.queues = [] self.logs = {} self.plot.reset() for node in self.series_ui.get_children(): self.series_ui.delete(node) # For every csv file in the directory, checking recursively and alphabetically for subdirname, subsubdirnames, filenames in os.walk(dirname): subsubdirnames.sort() filenames.sort() for filename in filenames: if filename.endswith(".csv") or filename.endswith(".log"): # The name of the directory without the name of the directory selected, # and the name of the file without the extension, separated by "."s # For example if directory selected is /tmp/logs, subdirname is /tmp/logs/beta/666, # and filename is foo.csv, nodeprefix is "beta.666.foo". nodeprefix = ".".join(subdirname[len(dirname)+1:].split("/") + [filename[:-4]]) if nodeprefix.startswith("."): nodeprefix = nodeprefix[1:] # Add the file's data self.readfile(subdirname + "/" + filename, nodeprefix) for name in self.names: # Add name to (name with everything after last dot removed), as the last child, # with text (everything after last dot of name) and tags graphnone and (whether name represents data) self.series_ui.insert(".".join(name.split(".")[0:-1]), "end", name, text=name.split(".")[-1], tags=["graphnone"]) # Open File button clicked def loadfile(self): filename = tkFileDialog.askopenfilename(filetypes=[("CSV Files", "*.csv"), ("Text log file", "*.log")], initialdir=log_dir) if filename != "" and filename != (): # Cancel and (x) not pressed # Empty the data self.used = [] self.series = {} self.names = [] self.queues = [] self.logs = {} self.plot.reset() for node in self.series_ui.get_children(): self.series_ui.delete(node) # Add the file's data self.readfile(filename, "") for name in self.names: # Add name to (name with everything after last dot removed), as the last child, # with text (everything after last dot of name) and tags graphnone and (whether name represents data) self.series_ui.insert(".".join(name.split(".")[0:-1]), "end", name, text=name.split(".")[-1], tags=["graphnone"]) # Add a file's data # nodeprefix is a string to add before every name in the file, represents file's name in the tree def readfile(self, filename, nodeprefix): try: if filename.endswith(".csv"): # For csv files this will always be concatenated with something if nodeprefix != "": nodeprefix += "." csvfile = open(filename, "rb") reader = csv.DictReader(csvfile) series = {} self.queues.append(nodeprefix[:-1]) for name in reader.fieldnames: # Add ancestor_names to names, without creating duplicates self.names = list(OrderedDict.fromkeys(self.names + ancestor_names(nodeprefix + name))) # Create a series for this field series[nodeprefix + name] = [] for row in reader: for name in reader.fieldnames: try: # Add cell to series if it represents a number series[nodeprefix + name].append(float(row[name])) except ValueError: # Not a number, no problem, could be game_specific_string or something pass self.series.update(series) else: self.names.append(nodeprefix) self.logs[nodeprefix] = open(filename, "r").readlines() except IOError: tkMessageBox.showerror(message="Could not open file: " + str(filename)) # Tree element was double clicked def onselect(self, _): series = self.series_ui.focus() # Set it to graph if no children are graphed, and not to graph if all or some are self.setgraphed(series, self.series_ui.tag_has("graphnone", series)) self.checkgraphed(series) # Set a node and its children to be either all or none graphed def setgraphed(self, node, shouldgraph): if shouldgraph: self.series_ui.item(node, tags=["graphall"]) # If the node represents data and it isn't already graphed, graph it if node in self.series and node not in self.used: self.used.append(node) # Timestamp is queue that contains node + ".timestamp" timestamp_name = [queue for queue in self.queues if node.startswith(queue)][0] + ".timestamp" self.plot.add_plot(self.series[timestamp_name], self.series[node], node) if node in self.logs and node not in self.used: self.used.append(node) self.plot.add_textlog(self.logs[node], node) # If nothing else is plotted, fit the plot to this if len(self.used) == 1: self.plot.fit_x() self.plot.fit_y() for child in self.series_ui.get_children(node): self.setgraphed(child, True) else: # Set tags to be (whether node represents data) and graphnone self.series_ui.item(node, tags=["graphnone"]) if node in self.used: self.used.remove(node) if node in self.logs: self.plot.remove_textlog(node) if node in self.series: self.plot.remove_plot(node) for child in self.series_ui.get_children(node): self.setgraphed(child, False) # Update the tags (and color) on a node and its ancestors, should always be called after setgraphed def checkgraphed(self, node): # Top level nodes are children of "" if node == "": return # True unless a child doesn't share this tag or there are no children graphall = True graphnone = True for child in self.series_ui.get_children(node): if not self.series_ui.tag_has("graphall", child): graphall = False if not self.series_ui.tag_has("graphnone", child): graphnone = False graphtag = "" if graphall and graphnone: # There are no children, check the used list instead graphtag = "graphall" if node in self.used else "graphnone" elif graphall: graphtag = "graphall" elif graphnone: graphtag = "graphnone" else: graphtag = "graphsome" # Set tags to be (whether node represents data) and the computed status self.series_ui.item(node, tags=[graphtag]) # Now that the status of this node is known, check the parent self.checkgraphed(self.series_ui.parent(node))
class Display: def __init__(self, controller): self.controller = controller self.currIndex = 0 # initialize the GUI self.app = Tk() self.app.title('Jack Magee\'s Pub') self.tree = Treeview(self.app, height=30) # name the tree columns, not sure if they have to be named numbers but that's how the example did it self.tree["columns"]=("one", "two", "three", "four") # set the column widths self.tree.column("one", width=200) self.tree.column("two", width=300) self.tree.column("three", width=200) self.tree.column("four", width=200) # set the column headings self.tree.heading("#0", text= "ID") self.tree.heading("one", text="Name") self.tree.heading("two", text="Order") self.tree.heading("three", text="Price") self.tree.heading("four", text="Respond (double-click)") self.tree.pack() # register handler for double-clicks self.tree.bind("<Double-1>", self.OnDoubleClick) def mainloop(self): self.app.mainloop() # this is like making tree entries buttons def OnDoubleClick(self, event): # get the pressed item item = self.tree.selection()[0] # get the item's text response = self.tree.item(item,"text") # this is the only response we are sending for now if response == 'rdy': # get the parent directory whose text is the customer id parent = self.tree.parent(item) customer_id = self.tree.item(parent,"text") # remove it from the tree self.tree.delete(parent) # send the message to the customer self.controller.send_msg(customer_id, response) # add a new order to the tree def takeOrder(self, customer_id, name, order, price): # just a row identifier thisRow = str(self.currIndex) # insert the i.d. and name at the top level self.tree.insert("", self.currIndex, thisRow, text=customer_id, values=(name, "", "", "")) # insert the "button" for sending notification to clients self.tree.insert(thisRow, 0, text='rdy', values=("", "", "", "Ready For Pick Up")) # this is a hacky solution to get multiline orders to appear because treeviews will # crop anything more than 1 line so I just make a new entry for every line multiline_order = order.split('\n') this_line = 1 for line in multiline_order[:-1]: # exclude the last end line if this_line == 1: # the first line has the name of the order and it's price self.tree.insert(thisRow, 1, text="order",values=("", order, price, "")) else: # just keep printing the extra options, sides, and add ons self.tree.insert(thisRow, this_line, text="order",values=("", line, "", "")) this_line += 1 self.currIndex += 1
class pylasticGUI(Frame): def __init__(self): self.__fileName = 'POSCAR' self.__NoP = 11 self.__strain = 0.05 self.__executable = '/home/t.dengg/bin/vasp/vasp.5.3/vasp' self.__status = None self.__finished = None self.__paths = None self.__structuresinst = None self.__p1 = None self.__p2 = None self.__workdir = os.getcwd() Frame.__init__(self, name = 'pylastic') self.grid(row=0, column=0, sticky=NSEW) #self.pack(expand=Y, fill=BOTH) self._create_panel() def _create_panel(self): panel = Frame(self, name='elastic') #panel.pack(side=TOP, fill=BOTH, expand=Y) panel.grid(row=0, column=0, sticky=NSEW) nb = Notebook(panel, name='notebook') nb.enable_traversal() #nb.pack(fill=BOTH, expand=Y, padx=2, pady=3) nb.grid(row=0, column=0, sticky=NSEW) self._create_setup_tab(nb) self._create_analyze_tab(nb) def _create_setup_tab(self, nb): frame = Frame(nb, name='setup') self.e1 = Entry(frame, width=7) self.e1.bind() self.e1.grid(row=0, column=1) self.e1.delete(0, END) self.e1.insert(0, '21') self.label1 = Label(frame, text='Number of points:') self.label1.grid(row=0, column=0, sticky=W) self.e2 = Entry(frame, width=7) self.e2.bind() self.e2.grid(row=1, column=1) self.e2.delete(0, END) self.e2.insert(0, '0.05') self.label2 = Label(frame, text='Max. Lagrangian strain:') self.label2.grid(row=1, column=0, sticky=W) #Choose inputfile (Button) b2 = Button(frame, text="Inputfile", width=15, command=self.select_file) b2.grid(row=3, column=0, columnspan=2) b = Button(frame, text="set values", width=15, command=self.read_val) b.grid(row=4, column=0, columnspan=2) #Read structure (Button) b1 = Button(frame, text="Read structure", width=15, command=self.read_POS) b1.grid(row=5, column=0, columnspan=2) #Write structures (Button) b3 = Button(frame, text="Setup calculation", width=15, command=self.setup_calc) b3.grid(row=6, column=0, columnspan=2) #Strart calculations b4 = Button(frame, text="Calculation status", width=15, command=self.check_calc) b4.grid(row=7, column=0, columnspan=2) v = IntVar() r11 = Radiobutton(frame, text="Energy", variable=v, value=1) r12 = Radiobutton(frame, text="Strain", variable=v, value=2, state=DISABLED) r11.select() r11.grid(row=8, column=0, sticky=W) r12.grid(row=9, column=0, sticky=W) w = IntVar() r21 = Radiobutton(frame, text="2nd", variable=w, value=1) r22 = Radiobutton(frame, text="3rd", variable=w, value=2) r21.select() r21.grid(row=10, column=0, sticky=W) r22.grid(row=11, column=0, sticky=W) nb.add(frame, text='Setup', underline=0, padding=2) return def _create_analyze_tab(self, nb): frame = Frame(nb, name='analyze') self._create_treeview(frame, ) root = self.tree.insert('', END, text = 'calc', values=['text1','text2']) #self._populate_tree(root) # b1 = Button(frame, text="Refresh", width=15, command=lambda: self._populate_tree(root)) b1.grid(row=2, column=0) b2 = Button(frame, text="Analyze", width=15, command=lambda: self.analyze(frame)) b2.grid(row=2, column=1) b2 = Button(frame, text="Result", width=15, command=lambda: self.create_window()) b2.grid(row=2, column=2) if self.__status and '--------' in self.__status: self.__finished = False else: self.__finished = True if self.__status and self.__finished: ECs.set_analytics() nb.add(frame, text='Analyze', underline=0, padding=2) return def _populate_tree(self, root, lock=None): if lock: lock.acquire() if len(self.tree.get_children(self.tree.get_children())) > 1: for i in self.tree.get_children(self.tree.get_children()): self.tree.delete(i) #self.tree.delete(self.tree.get_children()) status, paths = self.check_calc() for key in status.keys(): #print paths[key].lstrip(self.__workdir).split('/') self.tree.insert(root, END, text = status[key], values=['text3','text4', paths[key]]) if lock: lock.release() def _create_treeview(self, parent): f = Frame(parent) #f.pack(side=TOP, fill=BOTH, expand=Y) f.grid(row=0, column=0, sticky=NSEW, columnspan=3) # create the tree and scrollbars self.dataCols = ('fullpath', 'type', 'status') self.tree = Treeview(columns=self.dataCols, displaycolumns='status') ysb = Scrollbar(orient=VERTICAL, command= self.tree.yview) xsb = Scrollbar(orient=HORIZONTAL, command= self.tree.xview) self.tree['yscroll'] = ysb.set self.tree['xscroll'] = xsb.set # setup column headings self.tree.heading('#0', text='Directory Structure', anchor=W) self.tree.heading('status', text='Status', anchor=W) self.tree.column('status', stretch=0, width=100) # add tree and scrollbars to frame self.tree.grid(in_=f, row=0, column=0, sticky=NSEW) ysb.grid(in_=f, row=0, column=1, sticky=NS) xsb.grid(in_=f, row=1, column=0, sticky=EW) # set frame resizing priorities f.rowconfigure(0, weight=1) f.columnconfigure(0, weight=1) # action to perform when a node is expanded self.tree.bind('<<TreeviewOpen>>', self._update_tree) self.tree.bind("<Double-1>", self.OnDoubleClick) def OnDoubleClick(self, event): item = self.tree.identify('item',event.x,event.y) if self.tree.item(item,"text") == 'calc': self.create_window() def _update_tree(self, event): # user expanded a node - build the related directory nodeId = self.tree.focus() # the id of the expanded node if self.tree.parent(nodeId): # not at root topChild = self.tree.get_children(nodeId)[0] # if the node only has a 'dummy' child, remove it and # build new directory; skip if the node is already # populated if self.tree.item(topChild, option='text') == 'dummy': self.tree.delete(topChild) path = self.tree.set(nodeId, 'fullpath') self._populate_tree(nodeId, path, os.listdir(path)) def create_window(self): t = Toplevel(self) t.wm_title("Elastic constants") l = Label(t, text="Elastic constants:") l.grid(row=0, column=0) textf = Text(t) try: textf.insert(INSERT, self.ec.get_C()) except: textf.insert(INSERT, '') textf.grid(row=1, column=0) def read_val(self): self.__NoP = float(self.e1.get()) self.__strain = float(self.e2.get()) def read_POS(self): self.__poscar = POS(self.__fileName).read_pos() #print self.__poscar def read_strain(self): self.__strain = self.e2.get() def select_file(self): self.__fileName = askopenfilename() print self.__fileName self.__workdir = self.__fileName.rstrip('POSCAR') print self.__workdir def setup_calc(self): ###################### Create Structures instance: ################### self.__structuresinst = Structures() ## Generate distorted structures and add them to structures object: ## atom = ElAtoms() print self.__poscar atom.poscarToAtoms(self.__poscar) for etan in np.linspace(-self.__strain, self.__strain, self.__NoP): for strains in range(len(atom.strainList)): atom = ElAtoms() atom.poscarToAtoms(self.__poscar) atom.distort(eta=etan, strainType_index = strains) self.__structuresinst.append_structure(atom) ####################### Write vasp input files: ####################### self.__structuresinst.workdir = self.__workdir self.__structuresinst.write_structures(self.__structuresinst) #################### Start local vasp calculation: #################### if self.__executable: lock = thread.allocate_lock() self.__structuresinst.executable = self.__executable thread.start_new_thread(self.__structuresinst.calc_vasp, (lock,)) def check_calc(self): self.__status = {} self.__paths = {} if not self.__structuresinst: try: with open(self.__workdir+'/structures.pkl', 'rb') as input: structures = pickle.load(input) atoms = structures.get_structures() except: raise Exception("Please do setup first!") else: atoms = self.__structuresinst.get_structures() #print self.__structuresinst for stype, eta in atoms: #print stype, eta, atoms[(stype,eta)].path try: et.parse(atoms[(stype,eta)].path+'/vasprun.xml') self.__status[(stype,eta)] = 'finished' except: self.__status[(stype,eta)] = '--------' self.__paths[(stype,eta)] = atoms[(stype,eta)].path return self.__status, self.__paths def analyze(self, frame): if self.__status and '--------' in self.__status: self.__finished = False else: self.__finished = True if self.__status and self.__finished: self.ec = ECs() self.ec.set_structures() self.ec.set_gsenergy() self.ec.set_analytics() self.__p1 = self.ec.plot_cvs() self.__p2 = self.ec.plot_2nd() canvas = FigureCanvasTkAgg(self.__p1, master=frame) canvas.show() canvas.get_tk_widget().grid(row=3, column=0) canvas = FigureCanvasTkAgg(self.__p2, master=frame) canvas.show() canvas.get_tk_widget().grid(row=3, column=1) canvas._tkcanvas.grid(row=3, column=2)