Adding an option on the fly (Tkinter)

H

Harlin Seritt

I am making use of a Checkbutton widget. However, I would like to add
an option or method "on the fly." For example I would like to bind a
filename ("filename.txt") to a particular Checkbutton widget so that I
call it later as:

widget["filename"]

Is this possible to do?

Thanks,

Harlin
 
D

Diez B. Roggisch

Harlin said:
I am making use of a Checkbutton widget. However, I would like to add
an option or method "on the fly." For example I would like to bind a
filename ("filename.txt") to a particular Checkbutton widget so that I
call it later as:

widget["filename"]

Is this possible to do?

Yes. Use dicts to store these buttons so that you can later refer to them.
 
H

Harlin Seritt

Would I do this like:

buttondict = {button1, "name.txt"}

??

Thanks,

Harlin
 
D

Diez B. Roggisch

Harlin said:
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):
cbs[name].config(enabled=False)

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
code.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,007
Latest member
obedient dusk

Latest Threads

Top