class App_Button(object): """docstring for App_Button""" def __init__(self, master, text=None, styles=None, command=None, image=None, title=None, disabled=False): super(App_Button, self).__init__() self.master = master self.text = text self.styles = styles self.command = command self.image = image self.title = title self.disabled = disabled self.putButton() def putButton(self): self.btn_wrapper = LabelFrame(self.master, relief=FLAT, borderwidth=0) self.btn = Button(self.btn_wrapper, text=self.text, relief=FLAT, bg=self.styles['btn_bg'], padx=self.styles['padx'], pady=self.styles['pady'], fg=self.styles['btn_fg'], borderwidth=0, font=self.styles['big_font'], command=self.command, image=self.image, activeforeground=self.styles['a_fg'], activebackground=self.styles['a_bg'], cursor="hand2") self.btn.image = self.image if self.disabled: self.btn.bind("<Button-1>", lambda x: "break") else: self.btn.bind("<Enter>", self.mouseover) self.btn.bind("<Leave>", self.mouseout) self.btn.pack() if self.title is not None: self.tooltip = App_Tooltip(self.btn, text=self.title) def mouseover(self, event): self.btn.config(fg=self.styles['h_fg']) self.btn.config(bg=self.styles['h_bg']) def mouseout(self, event): self.btn.config(fg=self.styles['btn_fg']) self.btn.config(bg=self.styles['btn_bg']) def bind(self, *args, **kwargs): self.btn.bind(*args, **kwargs) def bind_wrapper(self, *args, **kwargs): self.btn_wrapper.bind(*args, **kwargs) def pack(self, *args, **kwargs): self.btn_wrapper.pack(*args, **kwargs) def place(self, *args, **kwargs): self.btn_wrapper.place(*args, **kwargs) def config(self, *args, **kwargs): self.btn.config(*args, **kwargs) def set_tooltip(self, text): self.tooltip.configure(text=text) def pack_forget(self): self.btn_wrapper.pack_forget() def place_forget(self): self.btn_wrapper.place_forget() def winfo_rootx(self): return self.btn_wrapper.winfo_rootx() def winfo_rooty(self): return self.btn_wrapper.winfo_rooty() def winfo_height(self): return self.btn_wrapper.winfo_height() def winfo_width(self): return self.btn_wrapper.winfo_width()
class App_Confirm(object): """docstring for App_Confirm""" def __init__(self, master, text, styles, root, size_determiner): super(App_Confirm, self).__init__() self.master = master self.text = text self.styles = styles.copy() self.size_determiner = size_determiner self.font_px_size = self.styles['big_font'].cget("size") self.root = root self.root_width = self.root.winfo_width() self.root_height = self.root.winfo_height() self.putConfirm() def putConfirm(self): self.overlay = LabelFrame(self.master, bg=self.styles['overlay'], fg=self.styles['overlay'], width=self.root_width, height=self.root_height, borderwidth=0) self.wrapper = LabelFrame(self.master, bg=self.styles['bg'], borderwidth=0, pady=19, padx=10) wraplength = int(len(self.size_determiner) * self.font_px_size) self.label = Label(self.wrapper, bg=self.styles['bg'], fg=self.styles['fg'], text=self.text, wraplength=wraplength, font=self.styles['big_font'], padx=18, pady=10) self.label.pack() choice_wrapper = LabelFrame(self.wrapper, bg=self.styles['bg'], borderwidth=0) choice_wrapper.pack() no_btn = App_Button(choice_wrapper, text="No", styles=self.styles, command=self.hide) no_btn.pack(side=LEFT, padx=10) self.yes_btn = App_Button(choice_wrapper, text="Yes", styles=self.styles) self.yes_btn.pack(side=LEFT) def hide(self): self.wrapper.place_forget() self.overlay.place_forget() def show(self, confirmed_command=None): self.wrapper.pack() self.root.update_idletasks() wrapper_width = self.wrapper.winfo_width() wrapper_height = self.wrapper.winfo_height() x = (self.root_width / 2) - (wrapper_width / 2) y = (self.root_height / 2) - (wrapper_height / 2) self.wrapper.place(x=x, y=y) self.yes_btn.config(command=confirmed_command) self.overlay.place(x=0, y=0) def update(self, text): self.label.config(text=text)
class App_Prompt(object): """docstring for App_Prompt""" def __init__(self, master, structure, styles, root, command): super(App_Prompt, self).__init__() self.master = master self.structure = structure self.command = command self.styles = styles.copy() self.font_px_size = self.styles['big_font'].cget("size") self.root = root self.root_width = self.root.winfo_width() self.root_height = self.root.winfo_height() self.putPrompt() def putPrompt(self): self.overlay = LabelFrame(self.master, bg=self.styles['overlay'], fg=self.styles['overlay'], width=self.root_width, height=self.root_height, borderwidth=0) self.wrapper = LabelFrame(self.master, bg=self.styles['bg'], borderwidth=0, pady=25, padx=19) self.widget_arr = [] for section in self.structure: row = LabelFrame(self.wrapper, bg=self.styles['bg'], borderwidth=0) row.pack(padx=10, pady=(0, 15)) label = Label(row, bg=self.styles['bg'], fg=self.styles['fg'], text=section['label'], font=self.styles['big_font']) label.pack(side=LEFT) widget_name = section['widget_class'] widget_args = section['widget_args'] if widget_name == "App_Num_Select": widget = App_Num_Select(row, **widget_args) widget.pack(side=LEFT) self.widget_arr.append(widget) choice_wrapper = LabelFrame(self.wrapper, bg=self.styles['bg'], borderwidth=0) choice_wrapper.pack() no_btn = App_Button(choice_wrapper, text="Cancel", styles=self.styles, command=self.hide) no_btn.pack(side=LEFT, padx=10) self.yes_btn = App_Button(choice_wrapper, text="Submit", styles=self.styles, command=self.exec_command) self.yes_btn.pack(side=LEFT) def hide(self): self.wrapper.place_forget() self.overlay.place_forget() def show(self, confirmed_command=None): self.wrapper.pack() self.root.update_idletasks() wrapper_width = self.wrapper.winfo_width() wrapper_height = self.wrapper.winfo_height() x = (self.root_width / 2) - (wrapper_width / 2) y = (self.root_height / 2) - (wrapper_height / 2) self.wrapper.place(x=x, y=y) self.yes_btn.config(command=confirmed_command) self.overlay.place(x=0, y=0) def exec_command(self): results = [] for widget in self.widget_arr: results.append(widget.get()) self.command(results) self.hide()
class App_Checkbox(object): """docstring for App_Checkbox""" def __init__(self, master, text, styles, checked=True, checkbox_id=None,oncheck=None, onuncheck=None): super(App_Checkbox, self).__init__() self.master = master self.text = text self.styles = styles self.checked = checked self.checkbox_id = checkbox_id self.oncheck = oncheck self.onuncheck = onuncheck self.num_toggles = 0 if self.checked else 1 self.putCheckbox() def putCheckbox(self): self.wrapper = LabelFrame(self.master, bg=self.styles['bg'], borderwidth=0, padx=5, pady=6, cursor="hand2") self.box = LabelFrame(self.wrapper, bg=self.styles['bg'], width=30, height=30, borderwidth=1, cursor="hand2") self.box.bind("<Button-1>", self.toggle_check) self.box.pack(side=LEFT) self.label = Label(self.wrapper, fg=self.styles['fg'] if self.checked else self.styles['placeholder_fg'], bg=self.styles['bg'], text=self.text, font=self.styles['font'], cursor="hand2") self.label.bind("<Button-1>", self.toggle_check) self.label.pack(side=LEFT, padx=6) self.check = Label(self.wrapper, fg=self.styles['fg'], bg=self.styles['bg'], text="✔", font=self.styles['checkbox_font'], cursor="hand2") if self.checked: self.check.place(x=4, y=3) self.check.bind("<Button-1>", self.toggle_check) def toggle_check(self, event): self.num_toggles += 1 if self.num_toggles % 2 == 0: self.check.place(x=4, y=3) self.label.config(fg=self.styles['fg']) if self.oncheck != None: self.oncheck(self.checkbox_id) else: self.check.place_forget() self.label.config(fg=self.styles['placeholder_fg']) if self.onuncheck != None: self.onuncheck(self.checkbox_id) def pack(self, *args, **kwargs): self.wrapper.pack(*args, **kwargs) def winfo_width(self, *args, **kwargs): return self.wrapper.winfo_width(*args, **kwargs) def scroll_bind(self, *args, **kwargs): self.wrapper.bind(*args, **kwargs) self.box.bind(*args, **kwargs) self.label.bind(*args, **kwargs) self.check.bind(*args, **kwargs)
class App_Popover(object): """docstring for App_Popover""" def __init__(self, master, styles): super(App_Popover, self).__init__() self.master = master self.styles = styles self.putPopover() def putPopover(self): self.wrapper = LabelFrame(self.master, bg=self.styles['bg'], borderwidth=0) self.arrow = Label(self.wrapper, text="→", font=self.styles['big_font'], bg=self.styles['bg'], fg=self.styles['fg_error']) self.arrow.pack(side=RIGHT, padx=4) self.text = Label(self.wrapper, text="", font=self.styles['big_font'], bg=self.styles['bg'], fg=self.styles['fg_error']) self.text.pack(side=RIGHT) def set_text(self, text): self.text.config(text=text) def move(self, target, anchor="left"): self.wrapper.pack() self.wrapper.update_idletasks() target_rootx = target.winfo_rootx() target_rooty = target.winfo_rooty() target_width = target.winfo_width() target_height = target.winfo_height() wrapper_width = self.wrapper.winfo_width() wrapper_height = self.wrapper.winfo_height() y_pos = target_rooty - (wrapper_height / 2) if anchor == "left": self.arrow.config(text="→") x_pos = target_rootx - wrapper_width self.arrow.pack(side=RIGHT, padx=4) self.text.pack(side=RIGHT) elif anchor == "right": self.arrow.config(text="←") x_pos = target_rootx + target_width self.arrow.pack(side=LEFT) self.text.pack(side=LEFT, padx=4) self.wrapper.place(x=x_pos, y=y_pos) def hide(self): self.wrapper.place_forget()