from tkinter import Button, PhotoImage

from . import utilities as utils
    
class PushButton(Button):

    def __init__(self, master, command, args=None, text="Button", icon=None, pady=10, padx=10, grid=None, align=None):

        # Description of this object (for friendly error messages)
        self.description = "[PushButton] object with text \"" + str(text) + "\""

        # Attempt to instantiate the object and raise an error if failed
        try:
            super().__init__(master)
        except AttributeError:
            utils.error_format( self.description + "\n" + 
            "The first argument was a " + str(type(master)) +". First argument must be [App] or [Box]")

        # If the args string was not blank, convert to allow args
        if args is not None:            
            command = utils.with_args(command, *args)


        # Try to instantiate a picture
        if icon is not None:
            try:
                img = PhotoImage(file=icon)  
                self.icon = img
                self.config(image=img, command=command, pady=pady, padx=padx)
            except:
                utils.error_format("Image import error - image must be a gif, check correct path")

        else:
            # Initial config on setup 
            self.config(text=text, command=command, pady=pady, padx=padx)
            
        # Pack or grid depending on parent
        utils.auto_pack(self, master, grid, align)


    # Change command
    def change_command(self, newcommand, args=None):
        if args is not None:            
            newcommand = utils.with_args(newcommand, *args)
        self.config(command=newcommand)

    # Change text
    def set_text(self, text):
        self.config(text=text)

    # Contributed by jezdean
    # -----------------------
    # Enable Button
    def enable(self):
        self.config(state=NORMAL)

    # Disable Button
    def disable(self):
        self.config(state=DISABLED)

    # Toggle button state 
    def toggle_state(self):
        button_state = self.cget("state")
        if button_state == "disabled":
            self.enabled()
        elif button_state == "normal":
            self.disabled()
    # -----------------------
            
    # Change padding
    def padding(self, padx, pady):
        self.config(padx=padx, pady=pady)

@@ -31,16 +31,34 @@
 otoImage
+, DISABLED, NORMAL
 %0A%0Afrom .

Fix #24