Newbie alert !

Eric Brunel eric_brunel at despammed.com
Fri Dec 3 07:15:35 EST 2004


Jean Montambeault wrote:
> I am not only learning Python but programming itself ; reading your 
> posts makes me believe that nobody is that much of a beginner here. Is 
> there a newgroup or list for my type somewhere I can't find it ?
> 
> To illustrate my case this script :
> 
> <CODE>
> 
> # function to draw rings for an Olympic flag
> def rings(size,offsetX,offsetY,coul):
>     x1,y1,x2,y2 = 170, 103, 170, 103,
>     can1.create_oval(x1-size-offsetX,y1+size+offsetY,\
>                      x2+size-offsetX,y2-size+offsetY,\
>                      width=8, outline=coul)
> 
> # **main****main****main****main****main****main**
> 
> fen1=Tk()
> can1=Canvas(fen1, bg='white', height=206, width=340)
> can1.pack(side=LEFT)
> 
> bou_europe=Button(fen1, text='Europe',\
>                   command=rings(41, 100, -22, 'blue'))

Here is what you do here: you *call* your "rings" function with the given 
parameters, and you assign the *result* of the function (which is None, since 
you do not have any "return" statement in rings) to the "command" option of your 
button. Since you do that for all buttons, all circles are drawn right away and 
no command is attached to any of your buttons.

The basic solution would be to create 5 different functions - one for each ring 
- and assign the function to the button's command option:

def ringEurope():
   rings(41, 100, -22, 'blue')
bou_europe = Button(fen1, text='Europe', command=ringEurope)

And so on...

Another shorter, but more difficult to understand solution would be to use an 
anonymous function built with lambda. Here is how it goes:

bou_europe = Button(fen1, text='Europe',
                     command=lambda: rings(41, 100, -22, 'blue'))

There are however some issues with lambda, so you'd probably better stick to the 
first solution.

[snip]
> bou_africa=Button(fen1, text='Africa',\
>                   command=rings(size=41, offsetX=0,offsetY=-22, 
> coul='black'))

(BTW, why do you pass the function arguments as positional parameters in the 
first call and as named parameters in the following ones? IMHO, since the 
parameters are positional in the function definition, you'd better pass them as 
such in the calls, i.e: rings(41, 0, -22, 'black')

(Et sinon, il existe un newsgroup Python francophone (fr.comp.lang.python) sur 
lequel tu seras le bienvenu si jamais tu préfères discuter en français)

HTH
-- 
- Eric Brunel <eric (underscore) brunel (at) despammed (dot) com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list