Adding an option on the fly (Tkinter)

Diez B. Roggisch deetsNOSPAM at
Sun Mar 6 13:20:18 CET 2005

Harlin Seritt wrote:

> Would I do this like:
> buttondict = {button1, "name.txt"}

If you want to.... but I guess the other way round makes more sense. And the
syntax is wrong, its supposed to be

{key : value, ...}

The point is that you can create buttons as much as you want - in a for loop
for example - but usually its important to keep _some_ reference to it so
you can alter it later - e.g. disable it. But you don't _have_ to, if you
don't want to touch it after creation. Something like this might give you
the idea:

cbs = {}
for name in filenames:
    # I don't know the real expression for creating a button - but you
should, and this illustrates the idea
    cb = check_button(parent, command=lambda name=name: do_something(name))
    cbs[name] = cb

Later, in do_something(name), you could e.g. say:

def do_something(name):

Of course this means that cbs has to be a global var. But there exist other
options of course - as part of a object or whatever. that depends on your

Diez B. Roggisch

More information about the Python-list mailing list