[Tkinter] messed callbacks
John Posner
jjposner at optimum.net
Wed Sep 9 13:42:14 EDT 2009
<snip>
> def cb12(): return output(c1+'->'+c2)
> def cb21(): return output(c2+'->'+c1)
I think these can be simplified, e.g:
def cb12(): output(c1+'->'+c2)
But I'd go with the functools.partial approach. You can save some code
by making output() do more of the work:
#---------------------------
from Tkinter import *
from functools import partial
def output(x,y,op):
if op == "<":
print x, "<---", y
elif op == ">":
print x, "--->", y
else:
print "Operation argument error!"
def doit(fr,lst):
for c1, c2 in zip(lst[::2], lst[1::2]):
subframe=Frame(fr)
Label(subframe,text=c1+' <->
'+c2).pack(side='left',expand=1,fill='both')
for op in ('<', '>'):
Button(subframe,text=op,
command=partial(output, c1, c2, op)).pack()
subframe.pack(fill='x',expand=1)
root=Tk()
doit(root,['pippo','pluto','paperino','zio_paperone'])
Button(root,text='Q',command=root.destroy).pack(expand=1,fill='x')
root.mainloop()
#---------------------------
-John
More information about the Python-list
mailing list