[Tkinter-discuss] Attaching Widget Var to Widget?

Lion Kimbro lionkimbro at gmail.com
Mon Nov 5 23:41:29 CET 2012


  Cute, I like it.

  I'm attaching the tkinter control variable to the tkinter widget object
for now, but I'll keep this in mind for the future.


On Mon, Nov 5, 2012 at 11:20 AM, Michael Lange <klappnase at web.de> wrote:

> On Sun, 4 Nov 2012 13:39:50 -0800
> Lion Kimbro <lionkimbro at 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 at python.org
> http://mail.python.org/mailman/listinfo/tkinter-discuss
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tkinter-discuss/attachments/20121105/12a0a54b/attachment.html>


More information about the Tkinter-discuss mailing list