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()
Beispiel #2
0
class App_Menu(object):
    """docstring for App_Menu"""
    def __init__(self, master, root, styles, structure):
        super(App_Menu, self).__init__()

        self.master = master
        self.structure = structure
        self.root = root

        self.styles = styles.copy()
        self.styles['big_font'] = styles['font']
        self.styles['btn_bg'] = styles['menu_bg']
        self.font_px_size = self.styles['big_font'].cget("size")

        self.putMenu()

    def putMenu(self):
        self.wrapper = LabelFrame(self.master, borderwidth=0)

        menu_padx = (self.root.winfo_width() / 2)
        menu_pady = (self.root.winfo_height() * 0.015)

        self.background = Label(self.wrapper,
                                fg=self.styles['menu_bg'],
                                bg=self.styles['menu_bg'],
                                font=self.styles['big_font'],
                                text=" ",
                                padx=menu_padx,
                                pady=menu_pady)
        self.background.pack()

        heading_offset = 0

        self.heading_arr = []
        self.offset_arr = []

        for heading in self.structure:
            text = heading['text']
            title = heading['title']
            command = heading['command']
            disabled = heading['disabled']

            heading_styles = self.styles.copy()
            if disabled:
                heading_styles['btn_fg'] = heading_styles['h_bg']

            _heading = App_Button(self.background,
                                  text=text,
                                  styles=heading_styles,
                                  command=command,
                                  title=title,
                                  disabled=disabled)

            _heading.place(x=heading_offset, y=0)
            self.offset_arr.append(heading_offset)
            self.heading_arr.append(_heading)

            # heading_offset += (len(text) * self.font_px_size) + self.styles['padx'] + 10
            self.root.update_idletasks()
            heading_offset += _heading.winfo_width()

    def pack(self, *args, **kwargs):
        self.wrapper.pack(*args, **kwargs)

    def winfo_height(self, *args, **kwargs):
        return self.wrapper.winfo_height(*args, **kwargs)

    def set_tooltip(self, index, text):
        self.heading_arr[index].set_tooltip(text=text)

    def set_text(self, index, text):
        self.heading_arr[index].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_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_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()