lundi 3 août 2015

What is the best way to add extensibility to the actions of a class in python

I am writing a super-class that has the code to generate a context menu on right-clicking a particular button.

When I/others subclass this, I want it to be trivial to add actions to this context menu, and not have to worry about re-writing the whole method to draw it.

What is the smoothest oop way to go about this in python?

def context_menu(self, image_name, event):
    menu = Tk.Menu(self.root, tearoff=0)
    menu.add_command(label="Copy Image Path", command= lambda name=image_name: self.to_clipboard(name))
    ## TODO add the ability to neatly add context options when defining a subclass.
    #for label, action in cls.context_options:
    #    menu.add(label=label, command=action(image_name))
    menu.post(event.x_root, event.y_root)

Aucun commentaire:

Enregistrer un commentaire