Partial Function Application -- Advantages over normal function?

woooee woooee at gmail.com
Mon Jul 18 21:23:09 CEST 2011


Partial can be used in a GUI program, like Tkinter, to send arguments
to functions.  There are other ways to do that as well as using
partial.  The following program uses partial to send the color to the
change_buttons function.
from Tkinter import *
from functools import partial

class App:
   def __init__(self, parent):
    self.my_parent = parent
    self.my_parent.geometry("200x100+10+10")

    self.R = list()
    for ctr, color in enumerate(("Red", "Blue", "Green")):
        btn = Radiobutton(self.my_parent, text=color, value=ctr+1,
                          command=partial(self.change_buttons, color))
        btn.grid(row = 2, column = ctr+1)
        btn.deselect()
        self.R.append(btn)
    self.R[0].select()
    self.change_buttons("Red")


   def change_buttons(self, color):
       self.my_parent.configure(bg=color)
       for btn in self.R:
            btn.configure(bg=color)

if __name__ == "__main__":
    root = Tk()
    root.title ("Color Option")
    app = App(root)
    root.mainloop()



More information about the Python-list mailing list