Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #9834
| From | Terry Reedy <tjreedy@udel.edu> |
|---|---|
| Subject | Re: Partial Function Application -- Advantages over normal function? |
| Date | 2011-07-18 16:17 -0400 |
| References | <mailman.1208.1310984052.1164.python-list@python.org> <adc8083f-bfb4-4929-b267-e8280b13403a@t8g2000prm.googlegroups.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.1231.1311020265.1164.python-list@python.org> (permalink) |
On 7/18/2011 3:23 PM, woooee wrote:
> 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)
This is a nice illustration. For future reference: enumerate now takes a
start value as second parameter. Given as 1, you do not need to remember
to add 1 for each usage.
for ctr, color in enumerate(("Red", "Blue", "Green"),1):
btn = Radiobutton(self.my_parent, text=color, value=ctr,
command=partial(self.change_buttons, color))
btn.grid(row = 2, column = ctr)
> 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()
--
Terry Jan Reedy
Back to comp.lang.python | Previous | Next — Previous in thread | Find similar | Unroll thread
Partial Function Application -- Advantages over normal function? Kurian Thayil <kurianmthayil@gmail.com> - 2011-07-18 15:43 +0530
Re: Partial Function Application -- Advantages over normal function? Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2011-07-19 01:08 +1000
Re: Partial Function Application -- Advantages over normal function? woooee <woooee@gmail.com> - 2011-07-18 12:23 -0700
Re: Partial Function Application -- Advantages over normal function? Terry Reedy <tjreedy@udel.edu> - 2011-07-18 16:17 -0400
csiph-web