Need help with Tkinter for dynamic # of objects

Tim Peters tim_one at email.msn.com
Mon Dec 13 03:43:16 EST 1999


[Donald Parker]
> ...
> I'm trying to define a Frame class that can have a variable
> number of button widgets. Within the Frame class I've tried
> coding the constructor as
>
> n=0
> while n < m :
>     self.set_of_buttons[n] = [Tkinter.Button(self)]
>     n+n+1
>
> ...which results an exception for an attribute error for
> 'set_of_buttons'

Do either

    self.set_of_buttons = [None] * m

before the loop and

    self.set_of_buttons[n] = Tkinter.Button(self)

within the loop; or

    self.set_of_buttons = []

before the loop and

    self.set_of_buttons.append(Tkinter.Button(self))

within the loop.  I assume "n+n+1" is a typo; also assume you really want
each element of set_of_buttons to be a button, not a 1-element list
containing a button

> I thought types and variables came into existence as a result of
> assignment,

There's your problem <wink>.  Attributes of objects come into existence as a
result of assignment, but while
    .set_of_buttons
is an attribute
    .set_of_buttons[n]
is not (it's the "[n]" mapping operator applied *to* the attribute
set_of_buttons, and the attribute must exist first else there's nothing to
apply [n] to).

AttributeError-never-lies-ly y'rs  - tim






More information about the Python-list mailing list