def __init__(self, *args, **kwargs): timer = kwargs.pop('timer') self.__timer = timer Toplevel.__init__(self, *args, **kwargs) interval = ParamItem(self) interval.labelText = 'Interval (ms)' interval.entryText = str(timer.interval) interval.entryWidth = 5 interval.pack(side=TOP) self.interval = interval self.active = IntVar(timer.active) Checkbutton(self, text='Activate', variable=self.active, command=self.onActivateClick).pack(side=TOP) Button(self, text='OK', command=self.onOKClick).pack(side=TOP) def hide(): self.visible = False self.protocol('WM_DELETE_WINDOW', hide) self.__visible = True self.visible = False
def askSpan(orient='v'): win = Toplevel() pxmin = ParamItem(win) pxmin.pack() pxmin.labelText = 'xmin' if orient == 'v' else 'ymin' pxmax = ParamItem(win) pxmax.pack() pxmax.labelText = 'xmax' if orient == 'v' else 'ymax' def formatter(val): val = float(val) val /= 100. return '{0:0.2f}'.format(val) alphaScale = LabeledScale(win, from_=0, to=100, name='alpha', formatter=formatter) alphaScale.set(50.0) alphaScale.pack() win.protocol('WM_DELETE_WINDOW', win.quit) win.focus_set() win.grab_set() win.mainloop() xmin = pxmin.entry.get() xmax = pxmax.entry.get() alpha = alphaScale.get() / 100. win.destroy() return map(float, (xmin, xmax, alpha))
def askSpan(orient="v"): win = Toplevel() pxmin = ParamItem(win) pxmin.pack() pxmin.labelText = "xmin" if orient == "v" else "ymin" pxmax = ParamItem(win) pxmax.pack() pxmax.labelText = "xmax" if orient == "v" else "ymax" def formatter(val): val = float(val) val /= 100.0 return "{0:0.2f}".format(val) alphaScale = LabeledScale(win, from_=0, to=100, name="alpha", formatter=formatter) alphaScale.set(50.0) alphaScale.pack() win.protocol("WM_DELETE_WINDOW", win.quit) win.focus_set() win.grab_set() win.mainloop() xmin = pxmin.entry.get() xmax = pxmax.entry.get() alpha = alphaScale.get() / 100.0 win.destroy() return map(float, (xmin, xmax, alpha))
def askgridprop(): win = Toplevel() color = ['#000000', '#000000'] propvars = [StringVar() for i in range(4)] guidata = ( { 'linestyle': ('Major Line Style', propvars[0], None), ######################################################################################################## 'linewidth': ('Major Line Width', propvars[1], checkPositiveFloat) }, { 'linestyle': ('Minor Line Style', propvars[2], None), 'linewidth': ('Minor Line Width', propvars[3], checkPositiveFloat) ######################################################################################################### }) for d in guidata: for key in d: pitem = ParamItem(win) pitem.pack() pitem.labelText = d[key][0] pitem.entry['textvariable'] = d[key][1] if d[key][2]: pitem.checkFunc = d[key][2] def setmajorcolor(): c = askcolor() color[0] = c[1] def setminorcolor(): c = askcolor() color[1] = c[1] Button(win, text='Major Line Color', command=setmajorcolor).pack() Button(win, text='Minor Line Color', command=setminorcolor).pack() win.protocol('WM_DELETE_WINDOW', win.quit) win.focus_set() win.grab_set() win.mainloop() win.destroy() c_major = StringVar() c_major.set(color[0]) c_minor = StringVar() c_minor.set(color[1]) guidata[0]['color'] = ('Major Line Color', c_major, None) guidata[1]['color'] = ('Minor Line Color', c_minor, None) return guidata
def askgridprop(): win = Toplevel() color = ["#000000", "#000000"] propvars = [StringVar() for i in range(4)] guidata = ( { "linestyle": ("Major Line Style", propvars[0], None), ######################################################################################################## "linewidth": ("Major Line Width", propvars[1], checkPositiveFloat), }, { "linestyle": ("Minor Line Style", propvars[2], None), "linewidth": ("Minor Line Width", propvars[3], checkPositiveFloat) ######################################################################################################### }, ) for d in guidata: for key in d: pitem = ParamItem(win) pitem.pack() pitem.labelText = d[key][0] pitem.entry["textvariable"] = d[key][1] if d[key][2]: pitem.checkFunc = d[key][2] def setmajorcolor(): c = askcolor() color[0] = c[1] def setminorcolor(): c = askcolor() color[1] = c[1] Button(win, text="Major Line Color", command=setmajorcolor).pack() Button(win, text="Minor Line Color", command=setminorcolor).pack() win.protocol("WM_DELETE_WINDOW", win.quit) win.focus_set() win.grab_set() win.mainloop() win.destroy() c_major = StringVar() c_major.set(color[0]) c_minor = StringVar() c_minor.set(color[1]) guidata[0]["color"] = ("Major Line Color", c_major, None) guidata[1]["color"] = ("Minor Line Color", c_minor, None) return guidata
def __init__(self, *args, **kwargs): #self._app = app = Application.instance self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) Button(self, text='Load *.mat', command=self.onLoadClick).pack() paramX = ParamItem(self) paramX.labelText = 'x = ' paramX.entryWidth = 6 paramX.pack() self.__paramX = paramX paramY = ParamItem(self) paramY.labelText = 'y = ' paramY.entryWidth = 6 paramY.pack() self.__paramY = paramY self.name = 'Load'
def askClassName(): win = Toplevel() moduleName = StringVar() className = StringVar() moduleItem = ParamItem(win) moduleItem.labelText = 'Module Name' moduleItem.pack() moduleItem.entryVar = moduleName classItem = ParamItem(win) classItem.labelText = 'Class Name' classItem.pack() classItem.entryVar = className Button(win, text='OK', command=win.quit).pack() win.protocol('WM_DELETE_WINDOW', win.quit) win.focus_set() win.grab_set() win.mainloop() win.destroy() return moduleName.get(), className.get()
def askClassName(): win = Toplevel() moduleName = StringVar() className = StringVar() moduleItem = ParamItem(win) moduleItem.labelText = "Module Name" moduleItem.pack() moduleItem.entryVar = moduleName classItem = ParamItem(win) classItem.labelText = "Class Name" classItem.pack() classItem.entryVar = className Button(win, text="OK", command=win.quit).pack() win.protocol("WM_DELETE_WINDOW", win.quit) win.focus_set() win.grab_set() win.mainloop() win.destroy() return moduleName.get(), className.get()
class EditGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) self.__center = ParamItem(frm) setMultiAttr(self.__center, labelText='center(deg)', entryText=0, checkFunc=self._app.checkInt, entryWidth=5, labelWidth=10) self.__center.pack(side=TOP) self._app.balloon.bind_widget( self.__center, balloonmsg='Specify the beam center here.') self.__width = ParamItem(frm) setMultiAttr(self.__width, labelText='width(deg)', entryText=20, checkFunc=self._app.checkInt, entryWidth=5, labelWidth=10) self.__width.pack(side=TOP) self._app.balloon.bind_widget( self.__width, balloonmsg='Specify the beam width here.') self.__uiImages = [] imageAddBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Add_Button.png')) self.__uiImages.append(imageAddBtn) btn = Button(frm, image=imageAddBtn, command=self.onAdd) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Add new beam to the ideal pattern.') imageDelBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Del_Button.png')) self.__uiImages.append(imageDelBtn) btn = Button(frm, image=imageDelBtn, command=self.onDel) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Remove the selected beam in the listbox.') imageClrBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Clear_Button.png')) self.__uiImages.append(imageClrBtn) btn = Button(frm, image=imageClrBtn, command=self.onClear) btn.pack(side=LEFT) self._app.balloon.bind_widget( btn, balloonmsg='Clear the listbox of the beam parameters.') imagePlotBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Plot_Button.png')) self.__uiImages.append(imagePlotBtn) btn = Button(frm, image=imagePlotBtn, command=self.onPlotIdealPattern) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Plot the ideal pattern.') frm.pack(side=LEFT, fill=Y) self.__paramlist = ScrolledList(self) self.__paramlist.list.config(height=4, width=10) self.__paramlist.pack(side=LEFT) self.name = 'Edit Ideal Pattern' self.optgrp = None def onAdd(self): self.__paramlist.list.insert( END, '{0}, {1}'.format(self.__center.getInt(), self.__width.getInt())) def onDel(self): self.__paramlist.list.delete(ANCHOR) def onClear(self): self.__paramlist.clear() def onPlotIdealPattern(self): printCode = True center, width = self.beamData self.__topwin.setIdealPattern(center, width) self.__topwin.plotIdealPattern() @property def beamData(self): beamParams = self.__paramlist.list.get(0, END) if not beamParams: self._app.printError('An error occurred!') self._app.printTip([{ 'type': 'text', 'content': '''This exception happens when the listbox of the beam parameters are empty. To make a valid ideal pattern, at least one beam should be specified. ''' }]) return center, width = zip( *[map(float, param.split(',')) for param in beamParams]) return center, width
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__uiImages = [] self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) frm.pack(side=TOP) imageMLbl = ImageTk.PhotoImage(file=uiImagePath('Pattern_M_Label.png')) self.__uiImages.append(imageMLbl) Label(frm, image=imageMLbl).pack(side=LEFT) self.__M = ParamItem(frm) self.__M.label.config(text='M') self.__M.entryWidth = 6 self.__M.entryText = 10 self.__M.entry.bind('<Return>', lambda dumb: self.onSolve()) self.__M.checkFunc = self._app.checkInt self.__M.pack(side=RIGHT) self._app.balloon.bind_widget( frm, balloonmsg='The number of the array elements.') imageSolveBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Solve_Button.png')) self.__uiImages.append(imageSolveBtn) self.__btnSolve = Button(self, image=imageSolveBtn, command=self.onSolve) self.__btnSolve.pack(side=TOP) self._app.balloon.bind_widget( self.__btnSolve, balloonmsg='Launch the solver to synthesize the correlation matrix.' ) frm = Frame(self) frm.pack(side=TOP) imageDisplayBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Display_Button.png')) self.__uiImages.append(imageDisplayBtn) Label(frm, image=imageDisplayBtn).pack(side=LEFT) self.__bDisplay = IntVar(0) chkDisplay = Checkbutton(frm, text="Display", variable=self.__bDisplay) chkDisplay.pack(side=TOP) self._app.balloon.bind_widget(frm, balloonmsg='Display solver output.') self.name = 'Optimize' def onSolve(self): printCode = True topwin = self.__topwin center, width = topwin.grpEdit.beamData topwin.figureBook.clear() topwin.setIdealPattern(center, width) topwin.plotIdealPattern() # Create a new thread for solving the correlation matrix. def solveFunc(): printCode = True topwin.solve(M=self.__M.getInt(), display=self.__bDisplay.get()) WaveSynThread.start(func=solveFunc) # Though method "start" will not return until the solve returns, the GUI will still # responding to user input because Tk.update is called by start repeatedly. # While the thread is not alive, the optimization procedure is finished. topwin.plotCurrentData()
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') super(OptimizeGroup, self).__init__(*args, **kwargs) paramFrm = Frame(self) paramFrm.pack(side=LEFT, expand=YES, fill=Y) self.__num = ParamItem(paramFrm) setMultiAttr(self.__num, labelText = 'num', entryText = '1', labelWidth = 5, entryWidth = 8, checkFunc = self._app.checkInt ) self.__num.entry.bind('<Return>', lambda event: self.onSolveClick()) self.__num.pack(side=TOP) self.__pci = ParamItem(paramFrm) setMultiAttr(self.__pci, labelText = 'PCI', entryText = '100', labelWidth = 5, entryWidth = 8, checkFunc = self._app.checkInt ) self.__pci.pack(side=TOP) self.__bParallel = IntVar() self.__chkParallel = Checkbutton(paramFrm, text="Parallel", variable=self.__bParallel) self.__chkParallel.pack() progfrm = Frame(self) progfrm.pack(side=LEFT, expand=YES, fill=Y) self.__genbtn = Button(progfrm, text='Generate', command=self.onSolveClick) self.__genbtn.pack(side=TOP) Button(progfrm, text='Stop', command=self.onStopBtnClick).pack(side=TOP) self.__progress = IntVar() self.__finishedwav = IntVar() self.__progbar = Progressbar(progfrm, orient='horizontal', variable=self.__progress, maximum=100) self.__progbar.pack(side=LEFT) self.__progbar.config(length=55) self.__finishedwavbar = Progressbar(progfrm, orient='horizontal', variable=self.__finishedwav) self.__finishedwavbar.pack(side=LEFT) self.__finishedwavbar.config(length=30) self.name = 'Generate' self.getparams = None self.__stopflag = False def onSolveClick(self): t1 = time.clock() if self.__bParallel.get(): self.parallelRun() else: self.serialRun() deltaT = time.clock() - t1 print(autoSubs('Total time consumption: $deltaT (s)')) def serialRun(self): tbicon = self._app.tbicon self.__stopflag = False wavnum = self.__num.getInt() progress = [0] waveform = [0] algorithm = self.__topwin.currentAlgorithm params = self.__topwin.grpParams.getParams() class AlgoThread(threading.Thread): def __init__(self, algo, params, waveform, progress): self.algo = algo self.progress = progress threading.Thread.__init__(self) def run(self): printCode = True self.algo.run(**params) self.__finishedwavbar['maximum'] = wavnum def progressChecker(k, K, y, *args, **kwargs): progress[0] = int(k / K * 100) algorithm.progressChecker.append(progressChecker) algorithm.progressChecker.interval = int(self.__pci.entryText) try: for cnt in range(wavnum): algothread = AlgoThread(algorithm, params, waveform, progress) algothread.start() while algothread.isAlive(): self.__progress.set(progress[0]) tbicon.progress = int((cnt*100+progress[0])/(wavnum*100)*100) self.__topwin.update() if self.__stopflag: break time.sleep(0.05) self.__progress.set(0) if self.__stopflag: break printCode = True self.__topwin.plotCurrentData() self.__finishedwav.set(cnt+1) finally: algorithm.progressChecker.remove(progressChecker) self.__finishedwav.set(0) tbicon.state = guicomponents.tk.TBPF_NOPROGRESS def parallelRun(self): class AlgoThread(threading.Thread): def __init__(self, algorithm, parameters, num): self.algorithm = algorithm self.parameters = parameters self.num = num super(AlgoThread, self).__init__() def run(self): printCode = True #parameters = Scripting.paramsToStr(**self.parameters) paramStr = evalFmt('[([], dict({Scripting.paramsToStr(**self.parameters)}))]*{self.num}') self.algorithm.parallelRunAndPlot(ScriptCode(paramStr)) algorithm = self.__topwin.currentAlgorithm parameters = self.__topwin.grpParams.getParams() theThread = AlgoThread(algorithm, parameters, self.__num.getInt()) theThread.start() while theThread.isAlive(): self.__topwin.update() time.sleep(0.05) def onStopBtnClick(self): self.__stopflag = True
class EditGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) self.__center = ParamItem(frm) setMultiAttr(self.__center, labelText = 'center(deg)', entryText = 0, checkFunc = self._app.checkInt, entryWidth = 5, labelWidth = 10 ) self.__center.pack(side=TOP) self._app.balloon.bind_widget(self.__center, balloonmsg='Specify the beam center here.') self.__width = ParamItem(frm) setMultiAttr(self.__width, labelText = 'width(deg)', entryText = 20, checkFunc = self._app.checkInt, entryWidth = 5, labelWidth = 10 ) self.__width.pack(side=TOP) self._app.balloon.bind_widget(self.__width, balloonmsg='Specify the beam width here.') self.__uiImages = [] imageAddBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Add_Button.png') ) self.__uiImages.append(imageAddBtn) btn = Button(frm, image=imageAddBtn, command=self.onAdd) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Add new beam to the ideal pattern.') imageDelBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Del_Button.png') ) self.__uiImages.append(imageDelBtn) btn = Button(frm, image=imageDelBtn, command=self.onDel) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Remove the selected beam in the listbox.') imageClrBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Clear_Button.png') ) self.__uiImages.append(imageClrBtn) btn = Button(frm, image=imageClrBtn, command=self.onClear) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Clear the listbox of the beam parameters.') imagePlotBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Plot_Button.png') ) self.__uiImages.append(imagePlotBtn) btn = Button(frm, image=imagePlotBtn, command=self.onPlotIdealPattern) btn.pack(side=LEFT) self._app.balloon.bind_widget(btn, balloonmsg='Plot the ideal pattern.') frm.pack(side=LEFT, fill=Y) self.__paramlist = ScrolledList(self) self.__paramlist.list.config(height=4, width=10) self.__paramlist.pack(side=LEFT) self.name = 'Edit Ideal Pattern' self.optgrp = None def onAdd(self): self.__paramlist.list.insert(END, '{0}, {1}'.format(self.__center.getInt(), self.__width.getInt())) def onDel(self): self.__paramlist.list.delete(ANCHOR) def onClear(self): self.__paramlist.clear() def onPlotIdealPattern(self): printCode = True center, width = self.beamData self.__topwin.setIdealPattern(center, width) self.__topwin.plotIdealPattern() @property def beamData(self): beamParams = self.__paramlist.list.get(0, END) if not beamParams: self._app.printError('An error occurred!') self._app.printTip( [ { 'type':'text', 'content':'''This exception happens when the listbox of the beam parameters are empty. To make a valid ideal pattern, at least one beam should be specified. ''' } ] ) return center, width = zip(*[map(float, param.split(',')) for param in beamParams]) return center, width
class OptimizeGroup(Group): def __init__(self, *args, **kwargs): self._app = Application.instance self.__uiImages = [] self.__topwin = kwargs.pop('topwin') Group.__init__(self, *args, **kwargs) frm = Frame(self) frm.pack(side=TOP) imageMLbl = ImageTk.PhotoImage( file=uiImagePath('Pattern_M_Label.png') ) self.__uiImages.append(imageMLbl) Label(frm, image=imageMLbl).pack(side=LEFT) self.__M = ParamItem(frm) self.__M.label.config(text='M') self.__M.entryWidth = 6 self.__M.entryText = 10 self.__M.entry.bind('<Return>', lambda dumb: self.onSolve()) self.__M.checkFunc = self._app.checkInt self.__M.pack(side=RIGHT) self._app.balloon.bind_widget(frm, balloonmsg='The number of the array elements.') imageSolveBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Solve_Button.png') ) self.__uiImages.append(imageSolveBtn) self.__btnSolve = Button(self, image=imageSolveBtn, command=self.onSolve) self.__btnSolve.pack(side=TOP) self._app.balloon.bind_widget(self.__btnSolve, balloonmsg='Launch the solver to synthesize the correlation matrix.') frm = Frame(self) frm.pack(side=TOP) imageDisplayBtn = ImageTk.PhotoImage( file=uiImagePath('Pattern_Display_Button.png') ) self.__uiImages.append(imageDisplayBtn) Label(frm, image=imageDisplayBtn).pack(side=LEFT) self.__bDisplay = IntVar(0) chkDisplay = Checkbutton(frm, text="Display", variable=self.__bDisplay) chkDisplay.pack(side=TOP) self._app.balloon.bind_widget(frm, balloonmsg='Display solver output.') self.name = 'Optimize' def onSolve(self): printCode = True topwin = self.__topwin center, width = topwin.grpEdit.beamData topwin.figureBook.clear() topwin.setIdealPattern(center, width) topwin.plotIdealPattern() # Create a new thread for solving the correlation matrix. def solveFunc(): printCode = True topwin.solve(M=self.__M.getInt(), display=self.__bDisplay.get()) WaveSynThread.start(func=solveFunc) # Though method "start" will not return until the solve returns, the GUI will still # responding to user input because Tk.update is called by start repeatedly. # While the thread is not alive, the optimization procedure is finished. topwin.plotCurrentData()