On Sun, 4 Nov 2012 13:39:50 -0800
Lion Kimbro <lionkim...@gmail.com> wrote:

> I'm trying to minimize maintenance of Var values. It is irritating that
> there is no method to get the check state of a Checkbutton, like there
> is to get the content of an Entry (.get).
> 
> There is much out there on using the variable= parameter out there, but
> little on strategies for maintaining the variables.
> 
> Are there any known problems with simply attaching a StringVar or
> IntVar to the Python widget it comes with?
> 
> Ex:
> 
> var=IntVar()
> chk=Checkbutton(frame, text="label", variable=var)
> chk.var=var
> 
> Will there be any allocation/deal location problems with this?
> If the widget is destroyed via a grandparent or something, will any
> resources be left dangling?  Anyone have any experience with this?
> 
> I notice in the tk docs that a default variable is created if you don't
> make one yourself, and I am considering creating a StringVar using the
> name parameter such that it matches by querying the full path from the
> ID of the widget.

If you have to handle a lot of checkbuttons in your application a custom
checkbutton class with its own associated variable might come in handy,
as in this example:

import tkinter

class CheckButton(tkinter.Checkbutton):
    def __init__(self, master, **kw):
        if 'variable' in kw:
            self._var = kw['variable']
        tkinter.Checkbutton.__init__(self, master, **kw)
        if not 'variable' in kw:
            self._var = tkinter.BooleanVar(self, value=False)
            self.configure(variable=self._var)
    def get(self):
        return self._var.get()
    def set(self, value):
        self._var.set(value)

root = tkinter.Tk()
cb = CheckButton(root, text='Test')
cb.pack(padx=100, pady=100)
def test(ev):
    print(cb.get())
root.bind('<F1>', test)
root.mainloop()


Regards

Michael


.-.. .. ...- .   .-.. --- -. --.   .- -. -..   .--. .-. --- ... .--. . .-.

You're dead, Jim.
                -- McCoy, "The Tholian Web", stardate unknown
_______________________________________________
Tkinter-discuss mailing list
Tkinter-discuss@python.org
http://mail.python.org/mailman/listinfo/tkinter-discuss

Reply via email to