class ManualIntegrationPage(Frame): def __init__(self, parent): Frame.__init__(self, parent) self.controller = parent title = Label(self,text="Manual Integration",font=("Arial",14,"bold")) title.grid(row=0,column=0,columnspan=6) self.f = Figure(figsize=(5,5), dpi=100) self.a = self.f.add_subplot(111) Label(self, text="Match to Element", font=("Arial",14)).grid(row=1,column=5) Label(self, text="Lower Bound: ").grid(row=7,column=0) self.lowerBound = Entry(self) self.lowerBound.grid(row=7,column=1) Label(self, text="Upper Bound: ").grid(row=7,column=2) self.upperBound = Entry(self) self.upperBound.grid(row=7,column=3) integrateButton = Button(self, text="Integrate", command = self.submit) integrateButton.grid(row=7,column=4) Button(self, text="Back",command=self.back).grid(row=8,column=0) self.curMenu = None def populate_values(self, energies, cps): self.energies = energies self.cps = cps self.a.cla() self.a.plot(energies, cps,"b-") self.canvas = FigureCanvasTkAgg(self.f, self) self.canvas.draw() self.canvas.get_tk_widget().grid(row=1,column=0, columnspan=5, rowspan=5) def add_peak_selector(self, poss): self.poss = poss if self.curMenu != None: self.curMenu.destroy() self.selected = StringVar(self.controller) self.selected.set("Select Element") opts = ["Select Element"] + [p[0]+": "+str(p[1]) for p in poss] #Stupid tkinter workaround self.curMenu = OptionMenu(self,self.selected,*opts) self.curMenu.grid(row=2, column = 5) def back(self): self.controller.show_frame(ReviewFitPage) def submit(self): try: lowBound = float(self.lowerBound.get()) highBound = float(self.upperBound.get()) except ValueError: messagebox.showinfo("Error","Use numbers for the bounds please!") return None if lowBound > highBound: messagebox.showinfo("Error","Lower bound cannot be greater than upper bound!") return None if lowBound < self.energies[0] or highBound > self.energies[-1]: messagebox.showinfo("Error","Bounds must be within data bound") return None if self.selected.get() == "Select Peak": messagebox.showinfo("Error","Select a peak match!") return None #do integration #TODO: Infinite loop here i think i=0 while self.energies[i]<lowBound: i += 1 j=i while self.energies[j]<highBound: j += 1 #trapezoidal sum startEnergy = self.energies[i] endEnergy = self.energies[j] n = j-i+1 area = (self.energies[j] - self.energies[i]) / (2 * (j-i+1)) * (sum(self.cps[i:j+1])+sum(self.cps[i+1:j])) peak = [] element, loc = self.selected.get().split(": ") for i in self.poss: if i[0] == element and str(i[1]) == loc: peak = i self.controller.add_mi_peak(peak, area, startEnergy, endEnergy, n) self.controller.increment_peak_counter() self.curMenu.destroy()
class CategoryManager(Frame): """Category manager for the sticky notes.""" def __init__(self, master, app, **kwargs): Frame.__init__(self, master, **kwargs) self.columnconfigure(0, weight=1) self.app = app self.style = Style(self) self.style.theme_use("clam") self.im_plus = PhotoImage(file=IM_PLUS) self.im_moins = PhotoImage(file=IM_MOINS) # --- Default category self.frame_def_cat = Frame(self) self.default_category = StringVar(self.frame_def_cat, CONFIG.get("General", "default_category").capitalize()) Label(self.frame_def_cat, text=_("Default category ")).grid(row=0, column=0, sticky="e", padx=(4, 0)) self.categories = CONFIG.options("Categories") self.categories.sort() categories = [cat.capitalize() for cat in self.categories] self.def_cat_menu = OptionMenu(self.frame_def_cat, self.default_category, CONFIG.get("General", "default_category").capitalize(), *categories) optionmenu_patch(self.def_cat_menu, self.default_category) self.def_cat_menu.grid(row=0, column=1, sticky="w", padx=4, pady=4) # --- Category colors, names ... self.frame_cat = Frame(self) self.colors = list(COLORS.keys()) self.colors.sort() self.images = [] for key in self.colors: self.images.append(PhotoImage(key, master=self, width=16, height=16)) fill(self.images[-1], COLORS[key]) self.cat_colors = {} self.cat_labels = {} self.cat_menus = {} self.cat_buttons = {} for i, cat in enumerate(self.categories): self.cat_labels[cat] = Label(self.frame_cat, text="%s" % cat.capitalize(), anchor='e') self.cat_labels[cat].grid(row=i + 2, column=0, sticky="ew", padx=2) self.cat_labels[cat].bind('<Double-Button-1>', self.change_name) self.cat_colors[cat] = StringVar(self) color = CONFIG.get("Categories", cat) self.cat_menus[cat] = OptionMenu(self.frame_cat, self.cat_colors[cat], INV_COLORS[color], *self.colors, command=lambda color, c=cat: self.change_menubutton_color(color, c), style="%s.TMenubutton" % cat) optionmenu_patch(self.cat_menus[cat], self.cat_colors[cat]) self.style.configure("%s.TMenubutton" % cat, background=color) self.cat_menus[cat].grid(row=i + 2, column=1, sticky="w", padx=4, pady=4) self.cat_buttons[cat] = Button(self.frame_cat, image=self.im_moins, command=lambda c=cat: self.del_cat(c)) self.cat_buttons[cat].grid(row=i + 2, column=2, padx=4, pady=4) self.add_cat_button = Button(self.frame_cat, image=self.im_plus, command=self.add_cat) self.add_cat_button.grid(row=i + 3, column=0, pady=(0, 4)) if len(self.categories) == 1: self.cat_buttons[self.categories[0]].configure(state="disabled") # --- placement self.frame_def_cat.grid(row=0, column=0, sticky="eswn") Separator(self, orient="horizontal").grid(row=1, columnspan=3, pady=10, sticky="ew") self.frame_cat.grid(row=2, column=0, sticky="eswn") def change_name(self, event): def ok(event): cats = [l.cget('text').lower() for l in self.cat_labels.values()] cat = name.get().strip().lower() if cat and cat not in cats: label.configure(text=cat.capitalize()) if old_cat == self.default_category.get(): self.default_category.set(cat.capitalize()) self.update_def_cat_menu() name.destroy() label = event.widget old_cat = label.cget('text') name = Entry(self, justify='center') name.insert(0, label.cget('text')) name.place(in_=label, relx=0, rely=0, anchor='nw', relwidth=1, relheight=1) name.bind('<FocusOut>', lambda e: name.destroy()) name.bind('<Escape>', lambda e: name.destroy()) name.bind('<Return>', ok) name.selection_range(0, 'end') name.focus_set() def get_color(self, category): return self.cat_colors[category].get() def get_name(self, category): return self.cat_labels[category].cget('text').lower() def change_menubutton_color(self, color, cat): """ change the color of the menubutton of the category cat when its default color is changed """ self.style.configure("%s.TMenubutton" % cat, background=COLORS[color]) def del_cat(self, category): rep = askyesnocancel(_("Question"), _("Do you want to delete all notes belonging to \ the category %(category)s? If you answer 'No', the category will be deleted but \ the notes will belong to the default category. Be careful, the change will take \ effect immediately and cannot be undone." % {"category": category})) if rep is not None: del(self.cat_colors[category]) self.cat_buttons[category].grid_forget() del(self.cat_buttons[category]) self.cat_labels[category].grid_forget() cat = self.cat_labels[category].cget('text') del(self.cat_labels[category]) self.cat_menus[category].grid_forget() del(self.cat_menus[category]) self.categories.remove(category) CONFIG.remove_option("Categories", category) if self.default_category.get() == cat: default = list(self.cat_labels.values())[0].cget('text') self.default_category.set(default) CONFIG.set("General", "default_category", default.lower()) self.update_def_cat_menu() if len(self.categories) == 1: self.cat_buttons[self.categories[0]].configure(state="disabled") if rep: self.app.delete_cat(category) self.app.update_notes() self.app.update_menu() save_config() def update_def_cat_menu(self): self.def_cat_menu.destroy() categories = [l.cget('text') for l in self.cat_labels.values()] self.def_cat_menu = OptionMenu(self.frame_def_cat, self.default_category, None, *categories) optionmenu_patch(self.def_cat_menu, self.default_category) self.def_cat_menu.grid(row=0, column=1, sticky="w", padx=4, pady=4) def add_cat(self): top = Toplevel(self) top.transient(self) top.grab_set() top.resizable(False, False) top.title(_("New Category")) def valide(event=None): cats = [l.cget('text').lower() for l in self.cat_labels.values()] cat = name.get().strip().lower() if cat and cat not in cats: i = self.add_cat_button.grid_info()['row'] self.add_cat_button.grid_configure(row=i + 1) self.cat_labels[cat] = Label(self.frame_cat, text="%s " % cat.capitalize()) self.cat_labels[cat].grid(row=i, column=0, sticky="e") self.cat_colors[cat] = StringVar(self, _("Yellow")) self.cat_menus[cat] = OptionMenu(self.frame_cat, self.cat_colors[cat], _("Yellow"), *self.colors, command=lambda color, c=cat: self.change_menubutton_color(color, c), style="%s.TMenubutton" % cat) self.style.configure("%s.TMenubutton" % cat, background=COLORS[_("Yellow")]) optionmenu_patch(self.cat_menus[cat], self.cat_colors[cat]) self.cat_menus[cat].grid(row=i, column=1, padx=4, pady=4) self.cat_buttons[cat] = Button(self.frame_cat, image=self.im_moins, command=lambda c=cat: self.del_cat(c)) self.cat_buttons[cat].grid(row=i, column=2, padx=4, pady=4) self.cat_buttons[self.categories[0]].configure(state="normal") self.categories.append(cat) self.categories.sort() self.update_def_cat_menu() top.destroy() name = Entry(top, justify="center") name.grid(row=0, column=0, columnspan=2, sticky="ew") name.bind("<Return>", valide) name.focus_set() Button(top, text="Ok", command=valide).grid(row=1, column=0, sticky="nswe") Button(top, text=_("Cancel"), command=top.destroy).grid(row=1, column=1, sticky="nswe")
class CategoryManager(Frame): """Category manager for the sticky notes.""" def __init__(self, master, app, **kwargs): """Create category manager.""" Frame.__init__(self, master, padding=4, **kwargs) self.columnconfigure(0, weight=1) self.rowconfigure(1, weight=1) self.app = app self.style = Style(self) self.style.theme_use("clam") self.im_plus = PhotoImage(file=IM_PLUS) self.im_delete = PhotoImage(file=IM_DELETE) # --- Default category self.frame_def_cat = Frame(self) self.default_category = StringVar( self.frame_def_cat, CONFIG.get("General", "default_category").capitalize()) Label(self.frame_def_cat, text=_("Default category ")).grid(row=0, column=0, sticky="e", padx=(4, 0)) self.categories = CONFIG.options("Categories") self.categories.sort() categories = [cat.capitalize() for cat in self.categories] self.def_cat_menu = OptionMenu( self.frame_def_cat, self.default_category, CONFIG.get("General", "default_category").capitalize(), *categories) optionmenu_patch(self.def_cat_menu, self.default_category) self.def_cat_menu.grid(row=0, column=1, sticky="w", padx=4, pady=4) # --- Category colors, names ... style = Style(self) style.configure('txt.TFrame', relief='ridge', border=2) bg = style.lookup('TFrame', 'background') frame = Frame(self, style='txt.TFrame', padding=1) frame.columnconfigure(0, weight=1) frame.rowconfigure(0, weight=1) txt = Text(frame, width=1, height=1, bg=bg, relief='flat', highlightthickness=0, padx=6, pady=6, cursor='arrow') scroll_x = AutoScrollbar(frame, orient='horizontal', command=txt.xview) scroll_y = AutoScrollbar(frame, orient='vertical', command=txt.yview) txt.configure(xscrollcommand=scroll_x.set, yscrollcommand=scroll_y.set) txt.grid(row=0, column=0, sticky='ewns') scroll_x.grid(row=1, column=0, sticky='ew') scroll_y.grid(row=0, column=1, sticky='ns') self.frame_cat = Frame(txt) txt.window_create('1.0', window=self.frame_cat) txt.configure(state='disabled') self.colors = list(COLORS.keys()) self.colors.sort() self.images = [] self.cat_colors = {} self.cat_labels = {} self.cat_menus = {} self.cat_buttons = {} for i, cat in enumerate(self.categories): self.cat_labels[cat] = Label(self.frame_cat, text="%s" % cat.capitalize(), anchor='e') self.cat_labels[cat].grid(row=i + 2, column=0, sticky="ew", padx=2) self.cat_labels[cat].bind('<Double-Button-1>', self.change_name) self.cat_colors[cat] = StringVar(self) color = CONFIG.get("Categories", cat) self.cat_menus[cat] = OptionMenu(self.frame_cat, self.cat_colors[cat], INV_COLORS[color], *self.colors, command=lambda color, c=cat: self. change_menubutton_color(color, c), style="%s.TMenubutton" % cat) optionmenu_patch(self.cat_menus[cat], self.cat_colors[cat]) self.style.configure("%s.TMenubutton" % cat, background=color) self.cat_menus[cat].grid(row=i + 2, column=1, sticky="w", padx=4, pady=4) self.cat_buttons[cat] = Button( self.frame_cat, image=self.im_delete, padding=0, command=lambda c=cat: self.del_cat(c)) self.cat_buttons[cat].grid(row=i + 2, column=2, padx=4, pady=4, sticky='ns') if len(self.categories) == 1: self.cat_buttons[self.categories[0]].configure(state="disabled") # --- placement self.frame_def_cat.grid(row=0, column=0, sticky="eswn", pady=4) frame.grid(row=1, column=0, sticky="eswn") Button(self, image=self.im_plus, command=self.add_cat).grid(row=2, column=0, sticky='w', pady=8) def change_name(self, event): """Change category name.""" def ok(event): cats = [l.cget('text').lower() for l in self.cat_labels.values()] cat = name.get().strip().lower() if cat and cat not in cats: label.configure(text=cat.capitalize()) if old_cat == self.default_category.get(): self.default_category.set(cat.capitalize()) self.update_def_cat_menu() name.destroy() label = event.widget old_cat = label.cget('text') name = Entry(self, justify='center') name.insert(0, label.cget('text')) name.place(in_=label, relx=0, rely=0, anchor='nw', relwidth=1, relheight=1) name.bind('<FocusOut>', lambda e: name.destroy()) name.bind('<Escape>', lambda e: name.destroy()) name.bind('<Return>', ok) name.selection_range(0, 'end') name.focus_set() def get_color(self, category): """Return color of category.""" return self.cat_colors[category].get() def get_name(self, category): return self.cat_labels[category].cget('text').lower() def change_menubutton_color(self, color, cat): """ Change the color of the menubutton of the category cat when its default color is changed. """ self.style.configure("%s.TMenubutton" % cat, background=COLORS[color]) def del_cat(self, category): """Remove category.""" rep = askyesnocancel( _("Question"), _("Do you want to delete all notes belonging to \ the category %(category)s? If you answer 'No', the category will be deleted but \ the notes will belong to the default category. Be careful, the change will take \ effect immediately and cannot be undone.") % {"category": category}) if rep is not None: del (self.cat_colors[category]) self.cat_buttons[category].grid_forget() del (self.cat_buttons[category]) self.cat_labels[category].grid_forget() cat = self.cat_labels[category].cget('text') del (self.cat_labels[category]) self.cat_menus[category].grid_forget() del (self.cat_menus[category]) self.categories.remove(category) CONFIG.remove_option("Categories", category) if self.default_category.get() == cat: default = list(self.cat_labels.values())[0].cget('text') self.default_category.set(default) CONFIG.set("General", "default_category", default.lower()) self.update_def_cat_menu() if len(self.categories) == 1: self.cat_buttons[self.categories[0]].configure( state="disabled") if rep: self.app.delete_cat(category) self.app.update_notes() self.app.update_menu() save_config() def update_def_cat_menu(self): """Update default caregory menu.""" self.def_cat_menu.destroy() categories = [l.cget('text') for l in self.cat_labels.values()] self.def_cat_menu = OptionMenu(self.frame_def_cat, self.default_category, None, *categories) optionmenu_patch(self.def_cat_menu, self.default_category) self.def_cat_menu.grid(row=0, column=1, sticky="w", padx=4, pady=4) def add_cat(self): """Add a category.""" top = Toplevel(self, class_='MyNotes') top.transient(self) top.grab_set() top.resizable(False, False) top.title(_("New Category")) def valide(event=None): cats = [l.cget('text').lower() for l in self.cat_labels.values()] cat = name.get().strip().lower() if cat and cat not in cats: c, i = self.frame_cat.grid_size() self.cat_labels[cat] = Label(self.frame_cat, text="%s" % cat.capitalize()) self.cat_labels[cat].grid(row=i, column=0, sticky="e") self.cat_colors[cat] = StringVar(self, _("Yellow")) self.cat_menus[cat] = OptionMenu( self.frame_cat, self.cat_colors[cat], _("Yellow"), *self.colors, command=lambda color, c=cat: self.change_menubutton_color( color, c), style="%s.TMenubutton" % cat) self.style.configure("%s.TMenubutton" % cat, background=COLORS[_("Yellow")]) optionmenu_patch(self.cat_menus[cat], self.cat_colors[cat]) self.cat_menus[cat].grid(row=i, column=1, padx=4, pady=4) self.cat_buttons[cat] = Button( self.frame_cat, image=self.im_delete, padding=0, command=lambda c=cat: self.del_cat(c)) self.cat_buttons[cat].grid(row=i, column=2, padx=4, pady=4, sticky='ns') self.cat_buttons[self.categories[0]].configure(state="normal") self.categories.append(cat) self.categories.sort() self.update_def_cat_menu() top.destroy() name = Entry(top, justify="center") name.grid(row=0, column=0, columnspan=2, sticky="ew") name.bind("<Return>", valide) name.bind("<Escape>", lambda e: top.destroy()) name.focus_set() Button(top, text="Ok", command=valide).grid(row=1, column=0, sticky="nswe") Button(top, text=_("Cancel"), command=top.destroy).grid(row=1, column=1, sticky="nswe")